美文网首页
页面传值1

页面传值1

作者: 行有余 | 来源:发表于2016-11-04 11:00 被阅读0次

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; min-height: 15.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px 'PingFang SC'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #703daa}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font: 13.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s11 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s12 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s13 {font: 13.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #008400}

    //
    // ViewController.swift
    // 页面传值
    //
    // Created by SZT on 2016/11/4.
    // Copyright © 2016年 SZT. All rights reserved.
    //

    import UIKit

    /*
    界面通信:界面传值
    (1)从前往后传
    -- 属性传值
    (2)从后往前传
    -- <1>代理传值,<2>closure(闭包)传值
    */
    class ViewController: UIViewController {
    var label:UILabel?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        label = UILabel(frame: CGRectMake(50, 150, 90, 30))
        label?.text = "海贼王"
        view.addSubview(label!)
        navigationItem.title = "FirstVC"
        let btn = UIButton(frame: CGRectMake(50,200,90,40))
        btn.backgroundColor = UIColor.grayColor()
        btn.setTitle("push", forState: .Normal)
        btn.addTarget(self, action: "pushToSecondVC", forControlEvents: .TouchUpInside)
        view.addSubview(btn)
        
        
    }
    
    func pushToSecondVC(){
        
        let secondVC = SecondViewController()
        //将当前的控制器设置为secondVC的代理
        secondVC.delegate = self
        //实例化
        //传值
        secondVC.labelStr = label?.text
        navigationController?.pushViewController(secondVC, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    }
    //设置了代理就一定要遵循协议
    extension ViewController:secondProtocol{
    func translateString(str: String) {
    label?.text = str
    }
    }

    相关文章

      网友评论

          本文标题:页面传值1

          本文链接:https://www.haomeiwen.com/subject/xfoiuttx.html