swift视图与控制器之间的简单传值

第一种 代码块(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下

class switfBlock: UIView{
    typealias testBlock = (String)->()//声明代码块
    var blo: testBlock? //代码块属性
    func nameBlock()  {
        self.blo!("fdasf fhjkasdf fajkfh")
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
}

2.使用方式

let  switfBlocks = switfBlock()
        switfBlocks.blo = {str in
            print("fa:\(str)")
        }

第二种 协议(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下

protocol swiftDelegateProtocol {//声明协议
    func testDelegate(str: String)//方法
}
class swiftDelegate: UIView {
     var delegatessde : swiftDelegateProtocol?//属性协议
    func namede()  {
        delegatessde?.testDelegate("fdasf\(swiftShare.shareDlin.names)")//开始传值
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
}

2使用方法

class ViewController: UIViewController, swiftDelegateProtocol {
    override func viewDidLoad() {
        super.viewDidLoad()
        let  swiftDelegates = swiftDelegate()
        swiftDelegates.delegatessde = self
        swiftDelegates .namede()
    }
    func testDelegate(str: String) {//调用协议
        print("de:\(str)")
    }
}

第三种 通知(oc)
1 新建一个swift文件(NSobject、view、controller等),代码如下
class swiftNotification: UIView {

func resigerNotifi()  {
    let notifi = NSNotificationCenter.defaultCenter()


    notifi.addObserverForName("notifid", object: nil, queue: nil,usingBlock: { (notifis: NSNotification) in
         print("no:\(notifi)")
    })//方式一

    let operationQueue = NSOperationQueue.mainQueue()
    notifi.addObserverForName("notifiQu", object: nil, queue: operationQueue) { (notofis: NSNotification) in
        print("noQ:\(notifi)")
    }//方式2️⃣
    //方式3
    notifi.addObserver(self, selector:#selector(self.notifiNmae(_:)), name: "nofiNmae", object: nil)
    notifi.addObserver(self, selector:#selector(notifiNmae(_:)), name: "nofiNmae", object: nil)

}
func notifiNmae(notifis: NSNotification) {
    let share = swiftShare.getdataShare2()
     print("noN:\(notifis)+_\(share.names)")
}

deist{//消除

NSNotificationCenter.defaultCenter().removeObserver(self)//移除通知
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
2 使用方式
NSNotificationCenter.defaultCenter().postNotificationName(“notifid”, object: nil)//方式1
NSNotificationCenter.defaultCenter().postNotificationName(“notifiQu”, object: nil)//方式2
NSNotificationCenter.defaultCenter().postNotificationName(“nofiNmae”, object: nil)//方式3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值