我是 Storyboard新手,在自定义初始化方面遇到问题。
在使用 Storyboard之前,我曾经这样做是从具有自定义初始化的表中推送 View Controller :
- (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];
}
现在,在 Storyboard中,我使用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];
}
}
我知道我可以更改我的私有(private)变量“hotel”并在 [segue destinationViewController] 之后设置属性,但我发现调用我的自定义 init 方法更有用。
有办法实现吗?
最佳答案
如果您在 init 之后需要执行大量代码,您可能需要为其创建一个单独的方法,然后在 initWithCoder:
和 中调用该方法>initWithFrame:
.
查看此答案以获取更多信息: Objective C - Custom view and implementing init method?
关于iphone - 使用 Storyboard 的 View Controller 的自定义初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12981953/