0

I have label in my secondviewcontroller. I would like to pass button index from firstviewcontroller to secondviewcontroller label. When I press button, it goes to second viewcontroller but label is nil

// FirstViewController.m

NSInteger index = [carousel indexOfItemViewOrSubview:sender];
int ind=index;
SecondViewController *sVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
sVC.myLabel.text=[NSString stringWithFormat:@"%d",ind];
[self presentModalViewController:sVC animated:YES];

//SecondViewController.h

@property (strong, nonatomic) IBOutlet UILabel *myLabel;

//SecondViewController.m

@synthesize myLabel;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%@",myLabel.text);

}
  • 1
    The issue is that the destination's view and all of its `IBOutlet` references have not been configured yet. You have to defer using the `IBOutlet` references until `viewDidLoad` in the destination view controller. So, that's why TooManyEduardos advises creating a new property to hold the value which `viewDidLoad` can reference. – Rob Dec 30 '14 at 17:11
  • 1
    Unrelated to your original question, `presentModalViewController` is deprecated in favor of `presentViewController`. Only use `presentModalViewController` if you need to support iOS versions prior to 5.0. – Rob Dec 31 '14 at 16:01
  • Thanks Rob. do you have any idea for the following question https://stackoverflow.com/questions/27723310/icarousel-scrolling-and-speed –  Dec 31 '14 at 16:24
  • 1
    I'm sure some iCarousel users will happen upon your question. I don't know that framework, so I can't offer advice without spending time digging through that library. By the way, don't forget to have `viewDidAppear` call the `super` method... – Rob Dec 31 '14 at 16:49
  • Thanks Rob for helping. –  Dec 31 '14 at 17:43

1 Answers1

2

In the SecondViewController.h add another property:

@property (nonatomic) NSInteger index;

Then in the FirstViewController.m pass the value of index to the index of the second view:

NSInteger index = [carousel indexOfItemViewOrSubview:sender];
int ind=index;  //now you don't need this

SecondViewController *sVC = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
sVC.myLabel.text=[NSString stringWithFormat:@"%d",ind];

// New line
sVC.index = index;

[self presentModalViewController:sVC animated:YES];
TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • Thanks, it works! Only thing is, it should be following @property (nonatomic) NSInteger *index, otherwise it returns an error. –  Dec 30 '14 at 17:09
  • 1
    Cool. May want to mark it as answered so it can help other folks in the future. – TooManyEduardos Dec 30 '14 at 17:09
  • Yeap, my bad. NSNumber requires a pointer but NSInteger does not. I just modified. – TooManyEduardos Dec 30 '14 at 17:14
  • What kind of reference counter would you use on NSInteger? Weak? – TooManyEduardos Dec 30 '14 at 17:15
  • The line that says `sVC.myLabel.text = ...` makes no sense. `myLabel` will undoubtedly be `nil` (that's the OP's original problem). Plus, you just advised he get rid of the `ind` variable that this line is using. I'd remove this entire line, as he'll be be doing that in `viewDidLoad` of the destination. – Rob Dec 30 '14 at 17:18