Swift 2 - for循环创建多个UIButtons

时间:2015-10-23 11:14:43

标签: ios xcode uibutton swift2

我有一个for循环,根据用户的注册方式创建多个按钮。

按钮创建正常,我已经为按钮添加了一个目标,需要打印按下哪个按钮传递值。

到目前为止,我有:

func loadUserVideos() {

        let userVideoNames = ["andrew", "john", "verya", "patry", "gabriy"]

        let countUserNames = userVideoNames.count - 1

        for index in 0...countUserNames {

            //print("\(index) times 5 is \(index * 5)")

            //UIBUTTON PLAY VIDEO USER
            let customButtonPlayVideoUser = UIButton(frame: CGRectMake(145, 32, 64, 64))
            let imageCustomUserBtn = UIImage(named: "video-play.png") as UIImage?
            customButtonPlayVideoUser.setImage(imageCustomUserBtn, forState: .Normal)
            customButtonPlayVideoUser.addTarget(self, action: "CustomUserVideobtnTouched:", forControlEvents:.TouchUpInside)
            customViewUser.addSubview(customButtonPlayVideoUser)


        }

    }

    func CustomUserVideobtnTouched(sender: UIButton, index: Int){

        print(index)
    }

如何将值传递给动作功能,以便应用程序可以看到按下了哪个按钮?

2 个答案:

答案 0 :(得分:3)

您可以使用标记值:

    func CustomUserVideobtnTouched(sender: UIButton){

        print(sender.tag)
    }

然后

mercurial/treediscovery.py

答案 1 :(得分:-1)

您可以将索引设置为按钮标签属性,并在选择器方法

中获取这样的索引
sender.tag
相关问题