无法使用按钮从一个视图控制器导航到另一个视图控制器

Cannot navigate from one view controller to another with button

它给出:

signal SIGABRT

日志中的另一个错误是:

libc++abi.dylib: terminating with uncaught exception of type NSException

@IBAction func loginok(sender: AnyObject) {
    if loginTextField?.text != "monal" && passwordTextField?.text  != "gosai"
    {
        warningLable?.text = "Invalid Credentials"

        loginTextField?.text = ""
        passwordTextField?.text=""
    }
    else
    {
        let View2 = self.storyboard!.instantiateViewControllerWithIdentifier("View2") as! ViewController2
         self.navigationController!.pushViewController(View2, animated: true)

    }
}

你需要为segue设置一个标识符,然后用

调用它
self.performSegueWithIdentifier("YourIdentifier", sender: self)

PerformSegueWithIdentifier 将不起作用,除非您在情节提要中设置了转场。如果你有,那么在你的 else 声明中,正如 Francisco 所说,你应该调用该函数。如果您还没有在 Storyboard 上设置 segue,那么您必须这样做才能使用此方法。

此外,按照惯例,您的 @IBAction 方法 header 应为 func loginok(sender: UIButton) {...}

在情节提要中单击 segue 并在 Utilities->Attributes inspector->Identifier 中键入名称,我使用 "mySegue" 在标识符下方,对于 Segue ,将其设为模态。

创建一个按钮并按住 ctrl 键拖动到 m 文件中: 这是我的代码:

    - (IBAction)nAction:(UIButton *)sender {
        self.mytext =  self.mytextfield.text;
       [self performSegueWithIdentifier:@"mySegue" sender:sender];
    }



    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if ([[segue identifier] isEqualToString:@"mySegue"])
        {
        NSLog(@" my segues is called mysegue");
        myViewController *controller = [segue destinationViewController] ;
        controller.textforlabel = self.mytext;
        }

不要忘记包含 h 文件,您在其中为 segue 定义目标 viewcontroller :)