简体   繁体   English

IOS 5 - 从另一个ViewController调用ViewController

[英]IOS 5 - Call a ViewController from another ViewController

As I'm very new to IOS development, I don't really know all the mechanisms that are to be used while developing iPhone apps. 由于我对IOS开发很陌生,我真的不知道在开发iPhone应用程序时要使用的所有机制。

Here, what I'm trying to do is to call another controller after performing a segue. 在这里,我要做的是在执行segue之后调用另一个控制器。

The context : 上下文 :

I have my first page, which basically consists on a login page, with a user/password system. 我有我的第一页,它基本上是一个登录页面,带有用户/密码系统。 I created a segue which is called by clicking on the Submit button. 我创建了一个segue,通过单击Submit按钮调用它。 Here's the code : 这是代码:

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

And here's the prepareForSegue function : 这是prepareForSegue函数:

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

The fact is that, when I click on the Submit button (on the login page), the logs show me that the user is correctly found, and then I get the error as following : 事实是,当我点击提交按钮(在登录页面上)时,日志显示用户被正确找到,然后我得到如下错误:

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'

Here's the storyboard : 这是故事板:

故事板

The second NavigationController is to be replaced with a Tab Bar Controller. 第二个NavigationController将替换为Tab Bar Controller。 But for some testing, I've put this one. 但是对于一些测试,我已经把它放了。

Could someone please guide me, telling me what I'm doing wrong and/or sending me some recent guides on storyboarding and navigation ? 有人可以指导我,告诉我我做错了什么和/或向我发送一些关于故事板和导航的最新指南?

Thanks a lot ! 非常感谢 !

By the looks of the error, it seems like segue.destinationViewController isn't what you think it is. 通过错误的外观,似乎segue.destinationViewController不是你想象的那样。 Sounds like you have a navigation controller in the mix. 听起来你有混合导航控制器。

It's a bit difficult to diagnose without your storyboard but try: 没有你的故事板,诊断有点困难,但尝试:

UINavigationController *nav = (UINavigationController*)segue.destinationViewController;
HomeController *home = (HomeController*)nav.topViewController;

If that doesn't work, you may want to place a breakpoint on the second line and take a look at the nab controller to see where the HomeController is on its stack. 如果这不起作用,您可能需要在第二行放置断点并查看nab控制器以查看HomeController在其堆栈中的位置。

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

 [self.navigationController pushViewController:projectManagementView animated:NO];

you can do it like in ios5: 你可以像在ios5中那样做:

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

you will create a segue from one of your view to another but not through buttons or anything else , just drag from your first-view to second-view your segue and when your process done just call the code above. 你将从你的一个视图到另一个视图创建一个segue,但不是通过按钮或其他任何东西,只需从你的第一个视图拖动到第二个视图你的segue,当你的过程完成时只需调用上面的代码。

hope this helps.. 希望这可以帮助..

edit : you need to give a name to your segue in your storyboard just clicking on it and then naming in on the right-side inspector.. 编辑:您需要在故事板中为您的segue命名,只需单击它然后在右侧检查器中命名即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM