简体   繁体   English

在新的Storyboard中将值传递给UIViewController - Swift

[英]Passing Values to UIViewController in new Storyboard - Swift

I am trying to pass values to a new view controller - located within a new storyboard file. 我试图将值传递给新的视图控制器 - 位于新的故事板文件中。 However when I do so, the result I get from the NewViewController is always nil. 但是当我这样做时,我从NewViewController得到的结果总是为零。

Below is how I show the view controller within the new storyboard: 下面是我在新故事板中显示视图控制器的方法:

// Show create account page with custom transition
var storyboard : UIStoryboard = UIStoryboard(name: StoryboardName, bundle: nil)
var vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier(NewViewController) as UIViewController

I try to send the information here: 我尝试在这里发送信息:

// Pass the delegate to the first view controller
let newViewController:NewViewController = NewViewController()
newViewController.createAccountDelegate = self
newViewController.teststring = "hello"

And then present the view controller. 然后呈现视图控制器。

vc.transitioningDelegate = self
vc.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(vc, animated: true, completion: nil)

Here is my NewViewController, where I try to receive the values. 这是我的NewViewController,我尝试接收值。 However end up still being nil. 但最终仍然是零。

import UIKit

class NewViewController: UIViewController {
    var createAccountDelegate:AccountCreationDelegate!
    var teststring:NSString!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

Am I sending the values incorrectly? 我错误地发送了这些值吗?

You are creating a new instance of NewViewController with this line 您正在使用此行创建NewViewController的新实例

let newViewController:NewViewController = NewViewController()

Instead assign variables and delegate to vc which you have instantiated from StoryBoard 而是将变量和委托分配给已从StoryBoard实例化的vc

var storyboard : UIStoryboard = UIStoryboard(name: StoryboardName, bundle: nil)
// It is instance of  `NewViewController` from storyboard 
var vc : NewViewController = storyboard.instantiateViewControllerWithIdentifier(NewViewController) as NewViewController

// Pass delegate and variable to vc which is NewViewController
vc.createAccountDelegate = self
vc.teststring = "hello"
vc.transitioningDelegate = self
vc.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(vc, animated: true, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值传递给模态UIViewController-Swift - passing values to a Modal UIViewController - Swift UITabBarController在情节提要的上方添加新的UIViewController(Swift) - UITabBarController add new UIViewController above storyboard ones (Swift) UIViewImage推送到故事板上的新UIViewController - UIViewImage push to new UIViewController IN STORYBOARD 如何快速使用代码移动到现有的UIViewController(在情节提要中) - How to move to an existing UIViewController (in storyboard) with code in swift 无法在 Swift Package 中显示 UIViewController 及其 Storyboard - Not able to present UIViewController with its Storyboard in a Swift Package Swift 中 UIViewController 的自定义初始化,在 storyboard 中设置接口 - Custom init for UIViewController in Swift with interface setup in storyboard 在故事板上使用Swift嵌套类作为UIViewController - Use Swift nested class as UIViewController in storyboard 使用Swift打开新的UIViewController - Open new UIViewController using Swift UIView子类在Swift中的UIViewController + Storyboard中设置自己的高度+约束 - UIView Subclass Set Own Height + Constraints In UIViewController + Storyboard in Swift 如何为没有故事板的 UIViewController (Swift) 提供标识符? - How to give an identifier for UIViewController (Swift) which has no storyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM