美文网首页
4.2.1、swift UIButton传多个值

4.2.1、swift UIButton传多个值

作者: 艾希_可可 | 来源:发表于2017-06-29 14:05 被阅读10次

classExtensionButton:UIButton{

varnewDict =NSMutableDictionary()

}

//方法一

funcdeliverBtn() {

letbtn =ExtensionButton(frame:CGRect(x:100, y:350, width:50, height:50))

self.view.addSubview(btn)

btn.backgroundColor=UIColor.yellow

btn.newDict=NSMutableDictionary(dictionary: ["name":"Tom","age":18])

btn.addTarget(self, action:#selector(deliverMuchData(sender:)), for: .touchUpInside)

}

funcdeliverMuchData(sender:ExtensionButton){

letdict = sender.newDict

letname = dict["name"]

print(sender.newDict,name ??"")

}

//方法二

funcrectBtn() {

letrectBtn =UIButton(frame:CGRect(x: (mainWith-80)/2, y:80, width:80, height:80))

self.view.addSubview(rectBtn)

rectBtn.backgroundColor=UIColor.red

//设置按钮可编辑

rectBtn.clipsToBounds=true

rectBtn.layer.cornerRadius=40

rectBtn.setImage(UIImage.init(named:"我的"), for: .normal)

rectBtn.layer.borderColor=UIColor.black.cgColor

rectBtn.layer.borderWidth=2

letname:NSString="Tom"

letage:Int=18

objc_setAssociatedObject(rectBtn,ViewController.RuntimeKey.nameKey, name, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

objc_setAssociatedObject(rectBtn,ViewController.RuntimeKey.ageKey, age, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

rectBtn.addTarget(self, action:#selector(touchActions(sender:)), for: .touchUpInside)

}

structRuntimeKey {

staticletnameKey =UnsafeRawPointer.init(bitPattern:"name".hashValue)

staticletageKey =UnsafeRawPointer.init(bitPattern:"age".hashValue)

}

functouchActions(sender:UIButton){

letname =objc_getAssociatedObject(sender,ViewController.RuntimeKey.nameKey)

letweight =objc_getAssociatedObject(sender,ViewController.RuntimeKey.ageKey)

print(name ??"a",weight ??"1")

}

相关文章

  • 4.2.1、swift UIButton传多个值

    classExtensionButton:UIButton{ varnewDict =NSMutableDicti...

  • OC与swift的数据传输

    简介 该项目主要介绍了oc与swift之间、swift内部几种常见的传值方式(属性传值、代码块传值、代理传值、通知...

  • 11.4页面传值

    //一、AppDelegate.swift // //AppDelegate.swift //页面传值 // //...

  • swift传值

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

  • OC和Swift比较

    OC与swift 1.协议 OC:主要用来传值 swift:不仅可以用来传值,swift中的协议可以定义属性方法,...

  • vue逆向传值

    vue返回上个页面逆向传值传多个值

  • Flutter_嵌套APP

    flutter 代码块 swift 传值 原生 to flutter 传值 flutte to 原生 写在后面...

  • Swift给button和view添加block回调

    UIButton-E.swift extension UIButton {private struct Runti...

  • iOS Block传值

    一:.h int type 是要传的值 可以跟多个值 多个值用,分开二:.m传值 三:接收值给cell 设置t...

  • Swift界面传值

    Swift中界面传值的方法 主要有三种 1.代理传值2.闭包传值(即OC中的Block) 属性传值 代理传值 F...

网友评论

      本文标题:4.2.1、swift UIButton传多个值

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