【问题标题】:Xcode 4.3.3 : how to assign a view controller while initializingXcode 4.3.3:如何在初始化时分配视图控制器
【发布时间】:2012-07-16 08:04:03
【问题描述】:

大家好。 由于我使用的是 IOS 5,因此我使用的是故事板。 在旧版本中,我可以轻松地写initWithNibName:@"Details",它就像一个魅力。 现在在故事板中,由于我没有使用任何 XIB 文件,我需要做同样的事情。 这是我的代码的 sn-p:

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

    //Get the selected country
    NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];

    //Initialize the detail view controller and display it.
    Details *dvController = [[Details alloc] initWithNibName:@"Details" bundle:nil];

    dvController.selectedAuthors = selectedAuthors;
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;
}

我的新片段:

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

    //Get the selected country
    NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard.storyboard" bundle:nil];
    Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard


    //Initialize the detail view controller and display it.
    //Details *dvController = [[Details alloc] init/*WithNibName:@"Details" bundle:nil*/];

    dvController.selectedAuthors = selectedAuthors;
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;
}

应用程序日志:

2012-07-17 16:30:15.760 AuthorsApp[6534:f803] WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <AuthorVC: 0x6857ba0>.  Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior.  This method will no longer be called in a future release.
2012-07-17 16:30:16.167 AuthorsApp[6534:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x6867750>) doesn't contain a view controller with identifier 'Details''
*** First throw call stack:
(0x1595022 0x1726cd6 0x500fef 0x3151 0x1675c5 0x1677fa 0x9fc85d 0x1569936 0x15693d7 0x14cc790 0x14cbd84 0x14cbc9b 0x147e7d8 0x147e88a 0xd6626 0x1dc2 0x1d35)
terminate called throwing an exception(lldb) 

最新申请日志:

2012-07-17 16:35:15.352 AuthorsApp[6600:f803] WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <AuthorVC: 0x688d810>.  Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior.  This method will no longer be called in a future release.
2012-07-17 16:35:15.912 AuthorsApp[6600:f803] Everything is ok now !
(lldb) 

最终记录

Couldn't register com.test.erc.AuthorsApp with the bootstrap server. Error: unknown error code.
This generally means that another instance of this process was already running or is hung in the debugger.(lldb) 

【问题讨论】:

    标签: xcode ios5 storyboard


    【解决方案1】:

    您可以像这样实例化位于情节提要中的控制器:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
    Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard
    

    另一个选项,因为您使用 Storyboards 将使用 segues 进行转换。 Here is a nice example。使用 segues 免费获得的一件事是目标控制器会自动为您实例化。委托方法如下所示:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if([[segue identifier] isEqualToString:@"Details"]){
             Details *dvController = (Details *)[segue destinationViewController];
             // Pass any data to destination controller
             // The transition is handled by the segue and defined in the Storyboard ('push' for example)
         }
    }
    

    编辑 这是一个屏幕截图,方便参考:

    【讨论】:

    • 谢谢你的帮助,但是这两种方法都不起作用..也许我做错了什么,你能用第二种方法写一个代码sn-p吗?
    • @EliasRahme 您是否在第一个示例中传递了正确的故事板名称和控制器标识符?对于第二个,我已经包含了一个带有完整工作示例的链接。
    • 是的,我做到了,我的故事板被命名为 MainStoryboard,请在上面找到我的新代码 sn-p 也许我遗漏了一些东西..但是应用程序会自动崩溃
    • 我什至尝试使用不带扩展名的 MainStoryBoard 名称
    • @EliasRahme 您不需要.storyboard 扩展。它应该只是MainStoryBoard。另外请确保您的控制器在情节提要中的标识符是正确的。如果不是,则引发异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2011-01-14
    相关资源
    最近更新 更多