iOS 5 - 如何从一个ViewController调用另一个ViewController

4
作为一个刚接触IOS开发的新手,我并不知道在开发iPhone应用时需要使用的所有机制。
这里,我的目的是在执行segue后调用另一个控制器。
背景:
我有一个登录页面,基本上是由用户/密码系统组成。我创建了一个segue,通过单击“提交”按钮来调用它。以下是代码:
- (IBAction)Connect:(UIButton *)sender
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"passwordsample", @"usernamesample", 
                          nil];

    if ((![[dict allKeys] containsObject:TFLogin.text])) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Login or password incorrect" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"Connection Not OK");
    }
    else {
        [self performSegueWithIdentifier:@"LoginSegue" sender:sender];
        NSLog(@"Connection OK");
    }
}

这里是 prepareForSegue 函数:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    HomeController *home = (HomeController *) segue.destinationViewController;
    home.sentLogin = TFLogin.text;
}

事实上,当我点击提交按钮(登录页面上的),日志显示用户已被正确找到,然后我会收到以下错误信息:
2012-04-30 11:24:44.630 AppName[1066:f803] -[UINavigationController setSentLogin:]: unrecognized selector sent to instance 0x6d73c30
2012-04-30 11:24:44.630 AppName[1066:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setSentLogin:]: unrecognized selector sent to instance 0x6d73c30'

这是故事板: Storyboard 第二个NavigationController将被替换为Tab Bar Controller。但为了进行一些测试,我放置了这个。
请问有人能指导我,告诉我我做错了什么,或者向我发送最近关于故事板和导航的指南吗?
非常感谢!
3个回答

0
从错误的表现来看,似乎segue.destinationViewController并不是你想象中的那样。听起来好像有一个导航控制器在其中。
没有你的故事板,诊断起来有点困难,但可以尝试:
UINavigationController *nav = (UINavigationController*)segue.destinationViewController;
HomeController *home = (HomeController*)nav.topViewController;

如果那不起作用,您可能需要在第二行上设置断点,并查看 nab 控制器以查看 HomeController 在其堆栈上的位置。

谢谢您的回答,我已经编辑了我的帖子,以展示我目前拥有的故事板。 - BMN

0

ProjectManagementViewController *projectManagementView = [[ProjectManagementViewController alloc] initWithNibName:@"ProjectManagementViewController" bundle:nil];

项目管理视图控制器 *projectManagementView = [[ProjectManagementViewController alloc] initWithNibName:@"ProjectManagementViewController" bundle:nil];
 [self.navigationController pushViewController:projectManagementView animated:NO];

我认为这是在IOS4中使用的,当时还没有故事板。我看到它仍然可以使用,但是随着故事板的使用,有更简单的方法来实现它。 - BMN

0

你可以像在iOS5中那样做:

[self performSegueWithIdentifier:@"your-segue-name" sender:self];

你需要创建一个从一个视图到另一个视图的Segue,但不是通过按钮或其他任何东西。只需从第一个视图拖动到第二个视图即可。完成后,调用上面的代码即可。
希望这可以帮助到你。
编辑:在Storyboard中,你需要给你的Segue命名,只需点击它然后在右侧检查器中进行命名即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接