swift - 在 Swift 中使用 PresentViewController 传递变量

标签 swift uiviewcontroller

我正在开发一个 swift 应用程序,我已经使用 presentViewController 创建了指向其他 View 的链接,但我不知道如何传递变量。如何将变量传递给新 View ?

@IBAction func WorkoutPressed(sender: AnyObject) {
    let storyBoard = UIStoryboard(name: "Main", bundle:nil)
    let workoutView = storyBoard.instantiateViewControllerWithIdentifier("workoutView") as WorkoutViewController
    self.presentViewController(workoutView, animated: false, completion: nil)
    //self.navigationController?.pushViewController(workoutView, animated: true)
}

谢谢

最佳答案

这比您想象的要容易:您不需要传递参数,因为您可以直接访问下一个 View Controller 的类变量。

也就是说,你可以这样做:

workoutView.myVar = myVal

并且,当该 View 加载时,myVar 将具有值 myVal。 (不过不要忘记在 WorkoutViewController 中声明它)。

关于swift - 在 Swift 中使用 PresentViewController 传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326870/

相关文章:

iphone - 如何用替换的 UINavigationController 推送 UIViewController

json - Error Domain=NSCocoaErrorDomain Code=4865 在 Swift 中从 API 调用解析 JSON

swift - XCode6 beta 6 Swift 编译器错误

ios - 根据类更改函数的返回类型

ios - 表达式类型不明确,没有更多上下文错误

ios - AlertController - 与 UIViewController 冲突

ios - 我们如何使用 Moya 调试/查看通过 API 设置的请求?

ios - 透明 ViewController 与 presentViewController 一起工作,但不与 pushViewController 一起工作

iphone - UITabBarController 触摸响应速度慢

ios - 无法弹出 iOS viewController。不确定,但我认为这与导航 Controller 有关