2012-07-17 93 views
-1

Hy all。 由于我使用IOS 5,因此我使用了故事板。 在旧版本中,我可以很容易地编写initWithNibName:@"Details",它的工作方式就像一个魅力。 现在在故事板中,由于我没有使用任何XIB文件,我需要做同样的事情。 这里是我的代码片段:Xcode 4.3.3:如何在初始化时分配视图控制器

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

回答

0

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

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

另一种选择,因为你用故事板是用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) 
    } 
} 

编辑 下面是简单的参考截图: enter image description here

+0

谢谢你的帮助,但没有这两种方法的worked..maybe IM做的事情错了,你能用第二种方法写一段代码吗? – 2012-07-17 13:18:14

+0

@EliasRahme您是否在第一个示例中传递了正确的故事板名称和您的控制器的标识符?对于第二个,我已经包含了一个具有完整工作示例的链接。 – Alladinian 2012-07-17 13:20:05

+0

是的,我做了,我的故事板被命名为MainStoryboard,并在上面找到我的新代码片段,也许我失去了一些东西..但该应用程序自动崩溃 – 2012-07-17 13:24:18

相关问题