我正在尝试以编程方式推送到下一个 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/