0

I'm using MMDrawerController And want passing data from one view controller to Another view controller as(HomeViewController to DetailViewController)

HomeViewController view controller

Category *categoryItem = [category objectAtIndex:cellIndex];
         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        DetailsViewController * centerVC=(DetailsViewController*)[storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
                    centerVC.catName= categoryItem.name ;
            centerVC.catUrl=categoryItem.url;
            UINavigationController * centerNav=[[UINavigationController alloc]initWithRootViewController:centerVC];
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            appDelegate.drawerController.centerViewController=centerNav;

And In ViewDidLoad We Want access property as DetailsViewController view controller DetailsViewController.h

@property (nonatomic,weak) NSString* catName;
@property (nonatomic,weak) NSString* catUrl;

- (void)viewDidLoad {
    [super viewDidLoad];

self.navigationItem.title = [_catName uppercaseString];

}

But some time _catName has value nil.

Is there anything wrong ,If Then plz suggest.

If problem with defining properties weak then what type property I choose for view controller property.

saurabh_mishra_08
  • 691
  • 1
  • 7
  • 14

4 Answers4

0

As you are passing data from appdelegate make your property as a strong reference. and this will also help you.

@property (nonatomic,strong) NSString* catUrl;
@property (nonatomic,strong) NSString* catName;

Differences between strong and weak in Objective-C

Community
  • 1
  • 1
Pankaj K.
  • 535
  • 8
  • 18
0

weak says "keep this as long as someone else points to it strongly". So your property catName and catUrl will sustain only until categoryItem is in memory.

Instead you can use copy.

@property (nonatomic, copy) NSString* catName;
@property (nonatomic, copy) NSString* catUrl;

A copy guarantees that the string you have will not change. Let's say you want to avoid something passing a mutable string and then changing it without you knowing.

For example,

NSMutableString *someRandomString = [NSMutableString stringWithString:@"stackoverflow"];

Entrepreneur * entrepreneur = [[Entrepreneur alloc] init];
entrepreneur.name = someRandomString;

[someRandomString setString:@"Steve Jobs"];

Now, the value of name property of Entrepreneur class will be dependent on whether the property is declared as strong or copy; it will be @"Steve Jobs" if the property is declared as strong, and @"stackoverflow" in case the property is declared as copy.

Anoop Nyati
  • 339
  • 2
  • 11
0

Of course the problem is with weak NSString property. You should have strong reference on NSString catName and NSString catUrl, because they have to be in memory. In you case you catName and catUrl are disappeared after method (where they had been set up) is completed.

But you should set a copy property parameter because of NSString (more about this here https://stackoverflow.com/a/31217735/5151981)

So your code should look like this:

@property (nonatomic, copy) NSString *catName;
@property (nonatomic, copy) NSString *catURLString;
Community
  • 1
  • 1
lebedac
  • 44
  • 3
0

please use nonatomic and retain

@property (nonatomic, retain) NSString* catUrl;
@property (nonatomic, retain) NSString* catName;
Kcs
  • 191
  • 1
  • 1
  • 4