【问题标题】:Cannot navigate from one view controller to another with button无法使用按钮从一个视图控制器导航到另一个视图控制器
【发布时间】:2015-06-24 13:09:29
【问题描述】:

它给出:

信号 SIGABRT

日志中的另一个错误说:

libc++abi.dylib:以 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)

    }
}

【问题讨论】:

    标签: ios swift cocoa-touch uiviewcontroller


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你需要给segue设置一个标识符,然后调用它

      self.performSegueWithIdentifier("YourIdentifier", sender: self)
      

      【讨论】:

      • 你能用像这样的 safe let 试试吗if let View2 = self.storyboard!.instantiateViewControllerWithIdentifier("View2") as? ViewController2 { self.navigationController!.pushViewController(View2, animated: true) } 也许错误在instantiateViewControllerWithIdentifier 而不是pushViewController,这不会解决你的问题问题,但也许会给你一个更好的方法
      • 这也是我要建议的。当我遇到这个问题时,这是由于输入错误,所以这里的问题可能是一样的。
      【解决方案3】:

      在情节提要中单击 segue,然后在 Utilities->Attributes inspector->Identifier 中键入名称,我使用了“mySegue” 在标识符下方,对于 Segue ,将其设为 Modal。

      创建一个按钮并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;
              }
      

      不要忘记包含定义 segue 目标视图控制器的 h 文件 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-06
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 2012-05-05
        • 2014-07-25
        • 2013-03-12
        • 1970-01-01
        相关资源
        最近更新 更多