Michael
Michael

Reputation: 1238

Passing data to ViewController which is content of UIPopoverController

Assuming that the project is using ARC. ContentViewController is content of UIPopoverController

- (IBAction)showPop:(UIButton *)button 
{
    _pressDate = [NSDate date];
    ContentViewController *cvc = [[InfoViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
    self.popController = [[UIPopoverController alloc] initWithContentViewController:cvc];
    cvc.dateLabel.text = [_pressDate description];
    [self.popController presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}

The code above works, no problem. But I've noted that if I call

cvc.dateLabel.text = [_pressDate description];

before

self.popController = [[UIPopoverController alloc] initWithContentViewController:cvc];

the label does not get an update. I just would like to understand what is the matter?

Upvotes: 0

Views: 568

Answers (1)

Martol1ni
Martol1ni

Reputation: 4702

There is a thumbrule that you should not edit the UI of a ViewController before viewDidLoad is run, becuase of ther reasons @Phillip Morris described.. Instead of setting the cvc.dateLabel.text directly before viewDidLoad is fired, declare a property textForDateLabel, and set cvc.textForDateLabel = [_pressDate description];. Then in viewDidLoad of your ContentViewController, do self.dateLabel.text = textForDateLabel;

Upvotes: 1

Related Questions