iphone - 使用 Storyboard 的 View Controller 的自定义初始化

标签 iphone uiviewcontroller storyboard uitableview

我是 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/

相关文章:

iphone - 我们如何在 iOS 应用程序中使用自定义字体?

iphone - 以编程方式打开iPhone的邮件客户端

ios - 保持导航 Controller 的状态,只改变 View

iphone - 如何执行转场

ios - 从 iOS App 中的音频文件中分离语音和音乐(制作卡拉 OK)

ios - 无法使用 SDWEBIMAGE 编译应用程序

objective-c - UIModalTransitionStyleFlipHorizo​​ntal 不起作用

ios - 从 unwind segue 中关闭 View Controller

iOS Storyboard : Statusbar color

storyboard - Xcode,它不能将上方或下方的 View 拖到界面生成器中的某个 View