swift传值

2017-06-02  本文已影响120人  初光夫

本文将介绍swift中的传值方式:属性传值、代理传值、闭包传值、通知传值
本文将在两个VC之间进行传值:HomeVC、PushVC

1.属性传值

属性传值是最常用的正向传值方式,下面过伪代码简单展示

//HomeVC中
  override func viewDidload() {
    super.viewDidLoad()
    let pushVC = PushVC()
    pushVC.str = "属性传值"
    //假设就在这里跳了,别太计较哦,下同
    self.navigationController?.pushViewController(pushVC,  animated : true)
  }

//PushVC中
  var str : String?
  override func viewDidLoad () {
    super.viewDidLoad()
    print(str!) //str -- 属性传值
  }

2.代理传值

废话不多说直接上代码

//HomeVC中
  class HomeVC : UIViewController, PushVCDelegate {
    override func viewDidLoad() {
      super.viewDidLoad()
      let pushVC = PushVC()
      pushVC.delegate = self
      self.navigationController?.pushViewController(pushVC, animated : true)
    }
    func pushDelegatePassValue(str : String) {
      print(str) //str--代理传值
    }
}

//PushVC中
  protocol PushVCDelegate {
    func pushDelegatePassValue(str : String)
  }
  class PushVC : UIViewController {
    var delegate : PushVCDelegate?

    //假设这里是button的点击事件
    func buttonClick {
      self.delegate?.pushDelegatePassValue(str : "代理传值")
      self.navigationController?.popViewController(animated : true)
    }
  }

3.闭包传值

闭包传值分两种情况:直接定义闭包传值、用typealias定义闭包类型传值
a.直接定义闭包传值

//HomeVC中
  class HomeVC : UIViewController {
    override func viewDidLoad() {
      let pushVC = PushVC()
      pushVC.closure = { (str) -> Void in
        print(str) //str -- 闭包传值
      }
      self.navigationController?.pushViewController(pushVC, animated : true)
    }
  }

//PushVC中
  class PushVC : UIViewController {
    let closure : ((String) -> Void)?
    //假设这里是button的点击事件
    func buttonClick {
      self.closure!("闭包传值")
      self.navigationController?.popViewController(animated : true)
    }
  }

b.用typealias定义闭包类型传值

//HomeVC中
  class HomeVC : UIViewController {
    override func viewDidLoad() {
      let pushVC = PushVC()
      pushVC.closurePassValue(closureType : { (str) -> Void in
        print(str) //str -- 闭包传值
      })
      self.navigationController?.pushViewController(pushVC, animated : true)
    }
  }

//PushVC中
  typealias Closure : (String) -> Void 
  class PushVC : UIViewController {
    var closure : Closure!
    //假设这里是button的点击事件
    func buttonClick {
      self.closure("闭包传值")
      self.navigationController?.popViewController(animated : true)
    }
    func closurePassValue (closureType : (String) -> Void) {
      self.closure = closureType
    }
  }

4.通知传值

通知传值和oc没啥区别,但是还是要写一下的

//HomeVC中
  class HomeVC : UIViewController {
    override func viewDidLoad() {
      super.viewDidLoad()
      
    }

  //假设这里是button的点击事件
  func buttonClick {
    let pushVC = PushVC()
    NotificationCenter.default.addObserver(forName : NSNotification.Name(rawValue : "haha"), object : nil, queue : OperationQueue.main) { (notify) in
        if let str = notify.userInfo?["666"] {
          print(String(describing : str)) //str -- 通知传值
        }
      }
      self.navigationController?.pushViewController(pushVC, animated: true)
    }

    deinit {
      NotificationCenter.default.removeObserver(self)
    }
  }

//PushVC中
  class PushVC : UIViewController {
    override func viewDidLoad() {
      super.viewDidLoad()
      
    }

  //假设这里是button的点击事件
    func buttonClick {
      NotificationCenter.default.post(name : NSNotification.Name(rawValue : "haha"), object: self, userInfo: ["666" : "通知传值"])
      self.navigationController?.popViewController(animated: true)
    }
  }
上一篇下一篇

猜你喜欢

热点阅读