swift - 从 IBAction 函数继续/推送到不同的 View Controller ?

标签 swift push viewcontroller ibaction sigabrt

我正在尝试以编程方式推送到下一个 View Controller ,以便我可以将数据保存在核心数据中,然后推送到下一个 View Controller 。基本上,当我点击带有 IBAction 的按钮时,它会执行所有这些操作。但是,每当我在 IBAction 函数中使用此代码时,我都会收到线程 1 SIGABRT 错误:

    @IBAction func onPlusTapped(){

        let alert = UIAlertController(title: "New Course", message: nil, preferredStyle: .alert)

        alert.addTextField { (textField) in
            textField.placeholder = "Course"
        }
        let action = UIAlertAction(title: "New", style: .default){ (_) in
            let course = alert.textFields!.first?.text!
            print(course)

            let coursy = Course(context: PersistenceService.context)
            coursy.name = course
            PersistenceService.saveContext()
        }
        alert.addAction(action)
        present(alert, animated: true, completion: nil)

        // jump to next view controller
        segway()

    }

    func segway(){
    let storyBoard: UIStoryboard = UIStoryboard(name: "viewcontroller", bundle: nil)
    let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "viewcontroller") as! ViewController
    self.present(balanceViewController, animated: true, completion: nil)
    }

}

最佳答案

如果您想在Course保存后跳转到下一个viewController,则必须在UIAlertAction完成中调用func segway() block 。

示例:

let action = UIAlertAction(title: "New", style: .default){ (_) in
            let course = alert.textFields!.first?.text!
            print(course)

            let coursy = Course(context: PersistenceService.context)
            coursy.name = course
            PersistenceService.saveContext()
            // jump to next view controller
            segway()

        }

还要仔细检查您的 Storyboard名称和balanceViewController的标识符是否为“viewcontroller”。

关于swift - 从 IBAction 函数继续/推送到不同的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53955807/

相关文章:

Swift - 我可以实现对类的相互泛型引用吗?

ios - 如何使 Xcode 中的 Swift 文件编译成为可选?

erlang - 如何使用 Erlang 发送推送通知?

git - git push --force 会删除远程文件吗?

ios - 导航 Controller 太多?

ios - 如何拖动和关闭 View Controller ?

ios - 调整视频大小并保持 1 :1 scale

javascript - 从 JavaScript.push 方法显示工具提示

ios - 将数据从 View Controller 传递到 tabController childs

ios - 如何在我的 TabViewController 之前添加登录屏幕