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

前端之家收集整理的这篇文章主要介绍了ios – 在新的Storyboard中将值传递给UIViewController – Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将值传递给新的视图控制器 – 位于新的故事板文件中.但是当我这样做时,我从NewViewController得到的结果总是为零.

下面是我在新故事板中显示视图控制器的方法

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

我尝试在这里发送信息:

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

然后呈现视图控制器.

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

这是我的NewViewController,我尝试接收值.但最终仍然是零.

import UIKit

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

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

错误地发送了这些值吗?

解决方法

您正在使用此行创建NewViewController的新实例
let newViewController:NewViewController = NewViewController()

而是将变量和委托分配给已从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,completion: nil)
原文链接:https://www.f2er.com/iOS/332270.html

猜你在找的iOS相关文章