2
votes

I'm new to storyboards and I'm having a problem with custom initialization.

Before using storyboards I used to do this to push a view controller from a table which has a custom initialization:

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

    //Creating Dummy Hotel
    HotelItem *hotel = [[HotelItem alloc] init];

    HotelDetailViewController *controller = [HotelDetailViewController controllerWithHotel:hotel];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];

    [hotel release];
}

Now with storyboards I'm using prepareForSegue instead of didSelectRowAtIndexPath which turned into this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    //Creating Dummy Hotel
    HotelItem *hotel = [[HotelItem alloc] init];
    hotel.name = @"Hotelucho";

    // Make sure we're referring to the correct segue
    if ([[segue identifier] isEqualToString:@"ShowSelectedHotel"]) {

        HotelDetailViewController *controller = [segue destinationViewController];

    }
}

I know that I can change my private variable "hotel" and set the property right after the [segue destinationViewController] but I find more useful to call my custom init method.

Is there a way to achieve this?

1

1 Answers

2
votes

IF you've got a big chunk of code you need to do after an init, you might want to create a separate method for it, and then call that method in initWithCoder: and initWithFrame:.

Check this answer for more info: Objective C - Custom view and implementing init method?