0

I'm french, so sorry for my bad English...

I want to pass data NSString *cellSelected. Here is my code, thanks to say what is wrong :)

#import "PhotoViewController.h"

@interface PhotoCategoryViewController : UITableViewController

@property (nonatomic, strong) NSArray *categoryName;

@property (nonatomic,strong) NSString *cellSelected;

@end

@implementation PhotoCategoryViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

  self.cellSelected = cell.textLabel.text;

  NSLog(@"selected cell textLabel = %@",self.cellSelected);

  PhotoViewController *viewcontroller = [[PhotoViewController alloc] init];

  viewcontroller.cellSelected = self.cellSelected;

}

and the second class :

#import "PhotoCategoryViewController.h"
   @interface PhotoViewController : UICollectionViewController     <UICollectionViewDataSource>

@property (nonatomic, strong) NSString *cellSelected;

- (void)viewWillAppear:(BOOL)animated
{

  NSLog(@"%@", self.cellSelected);
}

It's right for the first NSLog, but for the second (the passed data), that returns : "nil" ... :(

Binarian
  • 12,296
  • 8
  • 53
  • 84
Vjardel
  • 1,065
  • 1
  • 13
  • 28

2 Answers2

0

Welcome to Stackoverflow :)

for the second class:

 @property (nonatomic, readwrite, copy) NSString *cellSelected;

You need to specify it's intended to be written from the outside, with readwrite.
And the copy, to make sure the value of NSString is copied, and not referenced, which might get lost (This part caused me a lot of headache in the past year :) )

thedp
  • 8,350
  • 16
  • 53
  • 95
  • Hey, thanks for replying ! :) But I've add : "@property (nonatomic, readwrite, copy) NSString *cellSelected;" in PhotoViewController.h (the second class), and that returns nil again :( – Vjardel Jun 09 '14 at 16:40
  • Debatable since as I said it is readwrite by default. It certainly isn't causing the problem. – smyrgl Jun 09 '14 at 16:46
  • @Viny76 Since there is no segue, the ViewController is never displayed, and the `viewWillAppear` shouldn't even be called. For current debugging, try replacing `(void)viewWillAppear:(BOOL)animated` with `(void)viewDidLoad`. – thedp Jun 09 '14 at 16:54
  • @thedp the same for viewDidLoad :( – Vjardel Jun 09 '14 at 16:57
0

Your code doesn't make sense, there are a couple problems here:

  1. How are you presenting PhotoViewController? Your code doesn't show it...if you are using a segue to present the view controller then you would have two instances of it.
  2. Your PhotoViewController class shows an @interface definition but has a defined method in it suggesting it is an @implementation. Are you sure you are posting the right code?
  3. You should be using copy not strong references for your non-mutable properties.
  4. You don't need to declare UICollectionViewController as conforming to the UICollectionViewDatasource protocol, it does this by default.
smyrgl
  • 864
  • 6
  • 12
  • He doesn't have to segue for it to do what he wants it to do. – thedp Jun 09 '14 at 16:43
  • I know that but I don't know how he is presenting it since his code doesn't show it. I'm just pointing out that if he IS using a segue to present it then that would explain the issue. – smyrgl Jun 09 '14 at 16:44
  • And the `NSLog` is in the `viewWillAppear`... Is it even being called if there is no segue? – thedp Jun 09 '14 at 16:49
  • 1
    That's why I want to know how he is presenting it. If he is pushing or presenting his initialized instance of the PhotoViewController then this should work, but since the code isn't there I'm assuming that he might be dealing with a case where he is initializing a different instance of the view controller then what gets presented. – smyrgl Jun 09 '14 at 16:51
  • @smyrgl I haven't paste all my code, but he's right, no problem with interface and implementation. I can't use segue, because I use a UIBarButtonItem with conditions in UICollectionViewController for showing another TableView. So yes, i'm pushing to my new view. :) – Vjardel Jun 09 '14 at 16:54
  • 1
    Then can you explain the interface problems with PhotoViewController? It looks like an implementation file, right down to the #import of the PhotoViewController.h file. Are you sure you are declaring the property in the public header for PhotoViewController? You cannot use a class extension if you are setting it from an external class (well you can using KVC directly but that's not what you are doing). – smyrgl Jun 09 '14 at 16:56
  • @smyrgl Here is a screen from my storyboard : http://imageshack.com/a/img856/3572/nks0.png , I would like to send data PhotoCategoryViewController to PhotoViewController (I can make it by segue I think), AND AFTER to PhotoAdminViewController (no segue, here's my problem). – Vjardel Jun 09 '14 at 17:04
  • @smyrgl for PhotoCategoryViewController : `code` #import @interface PhotoCategoryViewController : UITableViewController @property (nonatomic, strong) NSArray *categoryName; @property (nonatomic,strong) NSString *cellSelected; @end for PhotoCategoryViewController.m : #import "PhotoCategoryViewController.h" #import "PhotoViewController.h" for photoviewcontroller.h : #import #import "PhotoCell.h" #import "PhotoAdminViewController.h" for photoviewcontroller.m: – Vjardel Jun 09 '14 at 17:12
  • #import "PhotoViewController.h" #import "PhotoDetailViewController.h" #import #import #import "PhotoCategoryViewController.h" – Vjardel Jun 09 '14 at 17:13