2012-01-24 34 views
0

我设计了我的代码,遵循iOS Developer Library中的CoreDataBooks示例。当按下UINavigationControl中的+ -Button时,会出现一个与自定义AddViewController相似的segue,类似于iOS AddressBook中的视图。如何初始化由UINavigationController提供的自定义视图?

我使用自定义视图控制器而不是UITableView控制器,如在CoreDataBooks中。所以呈现在CoreDataBooks视图控制器的代码如下所示:

AddViewController *addViewController = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
addViewController.delegate = self; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController]; 

[self.navigationController presentModalViewController:navController animated:YES]; 

和我看起来是这样的:

AddViewController *addViewController = [[AddViewController alloc] init]; 
[addViewController setDelegate: self]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController]; 

[self.navigationController presentModalViewController:navController animated:YES]; 

所以它基本上是相同的,但我初始化AddViewController与init和与由UITableViewController继承的initWithStyle方法。

如何正确初始化我的自定义视图控制器?我试图在init方法中调用[super init],但这没有帮助。

+0

您可以发布从AddViewController你的'init'方法? –

+0

最初没有init方法。我试图写一个,只是调用超类的init。 – Johannes

+0

对不起,我不知道什么时候调用了哪些初始化方法。如果有关于这个话题的好资源(特别是StoryBoards),我会很感激。 – Johannes

回答

0

因为您正在使用Storyboard来设计应用程序流程,所以您应该将故事板中的模态演示文稿定义为模式演示文稿,并在您的根视图控制器中给segue一个标识符并覆盖prepareForSegue:sender:

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

    if ([segue.identifier isEqualToString:@"MySegueIdentifier"]) { 

     AddViewController *destination = [[[segue destinationViewController] viewControllers] objectAtIndex:0]; 
     [destination setDelegate:self]; 
    } 
} 

+0

我知道可以在Storyboard中创建视图。不过,我想从代码中完成。如果我知道从Storyboard调用哪个init方法,我可以从代码调用相同的方法,并且我的问题将得到解决。 – Johannes

+0

..如果你使用故事板,你不应该尝试从代码实例化任何视图。 – Johannes