2012-10-19 75 views
2

我是新来的故事板,我遇到了自定义初始化的问题。使用故事板的视图控制器的自定义初始化

用故事板之前,我曾经这样做是为了从具有自定义初始化表推视图控制器:

- (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]; 
} 
与我使用prepareForSegue代替didSelectRowAtIndexPath方法故事板从而变成这个

现在:

- (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]; 

    } 
} 

我知道我可以更改我的私有变量“hotel”并在[segue destinationViewController]后面设置属性,但我发现调用我的自定义init方法更有用。

有没有办法做到这一点?

回答

2

如果您在初始化后需要执行大量代码,则可能需要为其创建单独的方法,然后在initWithCoder:initWithFrame:中调用该方法。

检查这个答案的详细信息: Objective C - Custom view and implementing init method?

+0

但是对于我上面贴的代码。如果我在initWithCoder和initWithFrame中进行了初始化,那么我如何通过像“controllerWithHotel:”那样的方法发送酒店变量? – Chompas

+0

在您的prepareForSegue ::方法中,只需在您的控制器对象上设置属性即可。不要忘记刷新设置属性后需要刷新的任何内容。 – Adis

+0

所以我不能保留我的酒店的财产私人,因为我不能使用像控制器的自定义InitWithHotel:对吧? – Chompas

相关问题