Search code examples
objective-ciospropertiesviewdidload

Setting a property in PrepareForSegue is null for ViewDidLoad but correct later on


I have searched up and down on this and found a few that were similar but not my exact problem. In my detail view controller I have a segue that opens a popover table view controller. In the PrepareForSegue method, I set a property of the table view, like so:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"StateSegue"])
    {
        popover = [(UIStoryboardPopoverSegue *)segue popoverController];
        StatePickerController *statePicker = (StatePickerController *)popover.contentViewController;
        statePicker.delegate = self;
        statePicker.pickerType = @"states";
    }
}

I pass both the delegate and the pickerType value. In my table view, StatePickerController, in the header I declared these properties like this:

@property (nonatomic,assign) id<StatePickerDelegate> delegate;
@property (nonatomic,strong) NSString *pickerType;

I know these values are getting passed into my popover controller correctly because later on in didSelectRowAtIndexPath I can reference them both and I get the right delegate and "state", however in the viewDidLoad method, both are still (null).

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
    self.states = [NSMutableArray array];
    NSLog(@"%@",self.delegate); //returns (null)
    NSLog(@"%@",self.pickerType); //returns (null)
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",self.pickerType); //corectly logs "state"
    NSString *state = [_states objectAtIndex:indexPath.row];
    [self.delegate stateSelected:state]; //correctly runs method on detailviewcontroller
}

Is there some point after the viewDidLoad method where these properties get set? I need access to these values right when the view loads because I need to determine what data populates in the table based on what is passed from the detailviewcontroller.


Solution

  • viewWillAppear is called after viewDidLoad. Use that instead to check the values after the prepareForSegue method.