这是一个工程,展示了如何在 VC 之间进行传值,包括正向传值、反向传值和无向传值。
- 本示例代码基于 Xcode 7 ,并使用Swift 2.0 写成。
![]()
正向传值
RootVC:
1 2 3
| let del:DelegateViewController = DelegateViewController() del.positiveValue = title! self.presentViewController(del, animated: true, completion: nil)
|
DelegateVC:
1
| var positiveValue:String = String()
|
反向传值
反向传值包括 delegate 、闭包、KVO 和 Notification 四种种方式:
Delegate
RootVC:
1 2 3 4 5
| @IBAction func delegateButtonDidTapped(sender: AnyObject) { ... del.delegate = self ... }
|
1 2 3 4
| func passValue(str:String) { self.delegateTF.text = str }
|
DelegateVC:
1
| var delegate:delegateOfNegative?
|
1 2 3 4 5
| func back(sender:UIButton) { let tf:UITextField = self.view.viewWithTag(10000) as! UITextField delegate?.passValue(tf.text!) self.dismissViewControllerAnimated(true, completion: nil) }
|
闭包
RootVC:
1 2 3 4 5 6 7 8 9 10
| @IBAction func blockButtonDidTapped(sender: AnyObject) { ... let blo:BlockViewController = BlockViewController() blo.passBlockValue = { (title:String) in self.positiveTF.text = title } self.presentViewController(blo, animated: true, completion: nil) }
|
BlockVC:
1
| var passBlockValue:((title:String) -> Void)?
|
1 2 3 4 5
| func back(sender:UIButton) { let tf:UITextField = self.view.viewWithTag(10001) as! UITextField passBlockValue?(title:tf.text!) self.dismissViewControllerAnimated(true, completion: nil) }
|
KVO
KVO只要是监听的属性,不管是正向还是反向都会触发observeValueForKeyPath
方法,在其中做相应的显示即可。
RootVC:
1
| var kvc:KVOViewController = KVOViewController()
|
1 2 3 4 5 6 7 8
| @IBAction func KVOButtonDidTapped(sender: AnyObject) { kvc.k = kvo() kvc.k.addObserver(self, forKeyPath: "title", options: [NSKeyValueObservingOptions.Old, NSKeyValueObservingOptions.New], context: nil) kvc.k.title = self.positiveTF.text! self.presentViewController(kvc, animated: true, completion: nil) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if keyPath == "title" { print(change!) var nv = change! let newvalue: AnyObject? = nv["new"] print("the new value is \(newvalue!)") self.positiveTF.text = "\(newvalue!)" } }
deinit { kvc.k.removeObserver(self, forKeyPath: "title", context: nil) }
|
KVOVC:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class kvo: NSObject { var ptitle : String = "" dynamic var title : String { get { return self.ptitle } set { self.ptitle = newValue } } override init() { println("init") } deinit { println("deinit") } }
|
1 2 3 4 5 6 7
| func back(sender:UIButton) { let tit = (self.view.viewWithTag(10003) as! UITextField).text
k.title = tit!
self.dismissViewControllerAnimated(true, completion: nil) }
|
Notification
RootVC:
viewDidLoad:
1 2
| NSNotificationCenter.defaultCenter().addObserver(self, selector: "notifReceive:", name: "notifName", object: nil)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @IBAction func NotificationButtonDidTapped(sender: AnyObject) { let noti:NotificationViewController = NotificationViewController() noti.positiveValue = self.positiveTF.text! self.presentViewController(noti, animated: true, completion: nil) }
func notifReceive(notification:NSNotification) { self.positiveTF.text = "\(notification.object!)" print("notif : \(notification.name), \(notification.object!)") }
deinit { ... NSNotificationCenter.defaultCenter().removeObserver(self, name: "notifName", object: nil) }
|
NotificationVC:
1 2 3 4 5 6 7 8
| func back(sender:UIButton) { let tit = (self.view.viewWithTag(10004) as! UITextField).text NSNotificationCenter.defaultCenter().postNotificationName("notifName", object: tit, userInfo: nil) self.dismissViewControllerAnimated(true, completion: nil) }
|
无向传值
其实就是利用NSUserDefaults
来存取数据,哈哈。
PS
- 所谓「反向传值」只是在业务逻辑上是从第二个 VC 回到第一个 VC 的过程中传值。并不是说列出的几种传值方式只能在反向情况下使用。
Demo:
The Why·Liam·Blog by WhyLiam is licensed under a Creative Commons BY-NC-ND 4.0 International License.
由WhyLiam创作并维护的Why·Liam·Blog采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于Why·Liam·Blog (https://blog.naaln.com),版权所有,侵权必究。
本文永久链接:https://blog.naaln.com/2016/12/different-ways-pass-value/