栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

轻松进行数据传递

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

轻松进行数据传递

ØyvindHauge用相同的解决方法击败了我,但正如我已经开始提供更详细的答案一样,我也会添加它。

假设您的两个视图控制器的命名如下:

主/入口:

ViewController (vcA)

次要视图:
ViewControllerB (vcB)

您(vcA) -> (vcB)可以按照示例中的步骤设置序列

// ...// segue ViewController -> ViewControllerBoverride func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!){    if segue.identifier == "viewNext" {        let viewControllerB = segue.destinationViewController as! ViewControllerB        viewControllerB.dataPassed = labelOne.text    }}

在有些麻烦步骤接下来的是,使用该方法,用于SEGUE
传递数据回从

(vcB)
to
(vcA)
is also added to the source of
(vcA)
, as an
@IBAction
方法(而不是象可能被预期的,添加到源
(vcB)
).

// ...// segue ViewControllerB -> ViewController@IBAction func unwindToThisView(sender: UIStoryboardSegue) {    if let sourceViewController = sender.sourceViewController as? ViewControllerB {        dataRecieved = sourceViewController.dataPassed    }}

You thereafter connect say, a button in

(vcB)
to this unwind action in
(vcA)
via the manual
Exit
segue in
(vcB)
:

Below follows a complete example of passing text from

(vcA)
to
(vcB)
;
(possibly) modifying that text via an
UITextField
, finally returning the
(possibly) modified text to
(vcA)
.


(vcA)
source:

import UIKitclass ViewController: UIViewController {    var dataRecieved: String? {        willSet { labelOne.text = newValue        }    }    @IBOutlet weak var labelOne: UILabel!    @IBAction func buttonOne(sender: UIButton) {        performSegueWithIdentifier("viewNext", sender: self)    }    // set default labelOne text    override func viewDidLoad() {        super.viewDidLoad()        labelOne.text = "Default passed data"    }    // segue ViewController -> ViewControllerB    override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)    {        if segue.identifier == "viewNext" { let viewControllerB = segue.destinationViewController as! ViewControllerB viewControllerB.dataPassed = labelOne.text        }    }    // segue ViewControllerB -> ViewController    @IBAction func unwindToThisView(sender: UIStoryboardSegue) {        if let sourceViewController = sender.sourceViewController as? ViewControllerB { dataRecieved = sourceViewController.dataPassed        }    }}

(vcB)
source (note that the
UITextFieldDelegate
delegate here is only used
for “locally” mutating the value of the
dataPassed
property, which will be
returned to
(vcA)
and assigned to
dataRecieved
property of the latter)

import UIKitclass ViewControllerB: UIViewController, UITextFieldDelegate {    var dataPassed : String?    @IBOutlet weak var textField: UITextField!    // set default textField text to the data passed from previous view.    override func viewDidLoad() {        super.viewDidLoad()        textField.text = dataPassed        // Handle the user input in the text field through delegate callbacks        textField.delegate = self    }    // UITextFieldDelegate    func textFieldShouldReturn(textField: UITextField) -> Bool {        // User finished typing (hit return): hide the keyboard.        textField.resignFirstResponder()        return true    }    func textFieldDidEndEditing(textField: UITextField) {        dataPassed = textField.text    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号