Xcode 4.3.3:初始化时如何分配视图控制器

时间:2012-07-17 11:47:46

标签: xcode ios5 storyboard

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

- (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) 

1 个答案:

答案 0 :(得分:0)

您可以像这样实例化位于故事板中的控制器:

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

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

-(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)
     }
}

修改 这是一个简单的参考截图: enter image description here