swift两个视图控制器之间的传值

swift两个视图控制器之间的传值,第1张

概述import UIKitclass ViewController: UIViewController, myDelegate { @IBOutlet weak var principalLabel: UILabel! @IBAction func mainButton(sender: UIButton) { //we got
import UIKitclass VIEwController: UIVIEwController,myDelegate {        @IBOutlet weak var principalLabel: UILabel!        @IBAction func mainbutton(sender: UIbutton) {                //we got it the final instance in storyboard        let secondController: SecondVIEwController = self.storyboard?.instantiateVIEwControllerWithIDentifIEr("SecondVIEwController") as! SecondVIEwController        secondController.data = "Text from superclass"        //who is it delegate        secondController.delegate = self        //we do push to navigate        self.navigationController?.pushVIEwController(secondController,animated: true)    }        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Do any additional setup after loading the vIEw,typically from a nib.    }        overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }        func writeDateInLabel(data:Nsstring){        self.principalLabel.text = data as String    }    }

当点击第二个视图控制器的按钮,就通过代理回传给第一个视图控制器的lable赋值

protocol myDelegate {    func writeDateInLabel(date:Nsstring)}class SecondVIEwController: UIVIEwController {        var data: Nsstring = ""    var delegate: myDelegate?        @IBOutlet weak var secondLabel: UILabel!        @IBAction func secondbutton(sender: AnyObject) {        self.delegate?.writeDateInLabel("I got it!")        self.navigationController?.popVIEwControllerAnimated(true)    }        overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()                // Do any additional setup after loading the vIEw.        secondLabel.text = data as String    }        overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }    }
总结

以上是内存溢出为你收集整理的swift两个视图控制器之间的传值全部内容,希望文章能够帮你解决swift两个视图控制器之间的传值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存