Ios 如何使用swift 3将数据从viewcontroller A传递到另一个viewcontroller B
我正在使用Ios 如何使用swift 3将数据从viewcontroller A传递到另一个viewcontroller B,ios,swift3,Ios,Swift3,我正在使用instanceviewcontrollerwhiteIdentifier(标识符:String)函数从ViewControllerA创建ViewControllerB的实例 let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB; rootController!.presentVi
instanceviewcontrollerwhiteIdentifier(标识符:String)
函数从ViewControllerA创建ViewControllerB的实例
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB;
rootController!.presentViewController(vc, animated: true, completion: nil)
class VCB: UIViewController {
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
}
我想访问我在ViewControllerB中传递的值如何实现这一点
我已经经历过了
链接但不链接目标c中的答案。您只需在
VCB
viewController中声明var
,并将数据注入此属性即可
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB;
vc.yourData = SOME_DATA
rootController!.presentViewController(vc, animated: true, completion: nil)
class VCB: UIViewController {
var yourData: AnyObject?
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
}
您只需在
VCB
viewController中声明var
,并将数据注入此属性
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("vcB") as VCB;
vc.yourData = SOME_DATA
rootController!.presentViewController(vc, animated: true, completion: nil)
class VCB: UIViewController {
var yourData: AnyObject?
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
}
你可以试试
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func passDataAction(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("UIViewControllerB") as! ViewControllerB;
vc.dataFromOtherView = "The data is passed"
self.presentViewController(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
另一班呢
import UIKit
class ViewControllerB: UIViewController {
var dataFromOtherView: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print(dataFromOtherView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
你可以试试
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func passDataAction(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("UIViewControllerB") as! ViewControllerB;
vc.dataFromOtherView = "The data is passed"
self.presentViewController(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
另一班呢
import UIKit
class ViewControllerB: UIViewController {
var dataFromOtherView: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print(dataFromOtherView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
只需使用此代码将数据从一个视图控制器发送到另一个视图控制器
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc=storyboard.instantiateViewControllerWithIdentifier("secondView") as! ViewControllerB;
vc.dataFromOtherView = "The data is passed"
self.presentViewController(vc, animated: true, completion: nil)
只需使用此代码将数据从一个视图控制器发送到另一个视图控制器
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc=storyboard.instantiateViewControllerWithIdentifier("secondView") as! ViewControllerB;
vc.dataFromOtherView = "The data is passed"
self.presentViewController(vc, animated: true, completion: nil)
我必须在viewcontrollerA中导入我的viewcontrollerB吗?@Ashok不需要。类型为“UIViewController”的值没有memebr“yourData”错误我有两个文件,一个是viewcontrollerA.swift,另一个是viewcontrollerB.swift我必须在viewcontrollerA中导入我的viewcontrollerB吗?@Ashok不需要。类型值“UIViewController”没有memebr“yourData”错误我有两个文件,一个是viewcontrollerA.swift,另一个是viewcontrollerB.swift有很多方法可以将数据从一个视图控制器传递到另一个视图控制器,如使用NSNotification center、委派、PrepareforSegue、,使用PropertyList您链接的问题也有很多快速的答案。有很多方法可以将数据从一个视图控制器传递到另一个视图控制器,如使用NSNotification center、委派、PrepareForegue、,使用PropertyList你链接的问题也有很多快速的答案。从哪里我必须调用passDataAction方法?从哪里我必须调用passDataAction方法?