-1

大家好。由于我使用的是 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) 
4

1 回答 1

0

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

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

由于您使用的是故事板,因此另一个选择是使用segues进行过渡。这是一个很好的例子。使用 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)
     }
}

编辑 这是一个便于参考的屏幕截图: 在此处输入图像描述

于 2012-07-17T12:57:55.417 回答