Ios 获取一个字符串并将其传递给代理

Ios 获取一个字符串并将其传递给代理,ios,swift,Ios,Swift,我搜索了很多,但没有找到答案,我将非常感谢你的帮助! 我需要获取一个字符串并将其传递到另一个位置,但我不知道如何在我的情况下执行该操作 有两种方法 方式1 将数组索引指定给按钮标记。点击操作获取索引并从索引中获取数据 像这样 func addTagLabels() { for (index, tag) in hashtags.enumerated() { let button = UIButton(type: .system) button.tag =

我搜索了很多,但没有找到答案,我将非常感谢你的帮助! 我需要获取一个字符串并将其传递到另一个位置,但我不知道如何在我的情况下执行该操作

有两种方法

方式1

将数组索引指定给按钮标记。点击操作获取索引并从索引中获取数据

像这样

func addTagLabels() {
    for (index, tag) in hashtags.enumerated() {
        let button = UIButton(type: .system)
        button.tag = index //<--- Here assign index
        
       // Other code
    }
}
class CustomButton: UIButton {
    var data: String = ""
}

func addTagLabels() {
    for tag in hashtags {
        let button = CustomButton()
        button.setTitle("#\(tag)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
        button.data = tag //<-- Here
        // Other code
    }
}

@objc func didTap(_ sender: UIButton) {
    if let btn = sender as? CustomButton {
        delegate?.didTap(tag: btn.data)
    }
}

方式2

创建自定义UIButton子类并传递值。像这样

class CustomButton: UIButton {
    var data: String = ""
}

func addTagLabels() {
    for tag in hashtags {
        let button = CustomButton()
        button.setTitle("#\(tag)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
        button.data = tag //<-- Here
        // Other code
    }
}

@objc func didTap(_ sender: UIButton) {
    if let btn = sender as? CustomButton {
        delegate?.didTap(tag: btn.data)
    }
}

class自定义按钮:ui按钮{
变量数据:String=“”
}
func addTagLabels(){
用于hashtags中的标记{
let button=CustomButton()
setTitle(“#\(标记)”,表示:。正常)
addTarget(self,action:#选择器(didTap),for:.touchUpInside)
button.data=tag/有两种方法

方式1

将数组索引分配给按钮标记。然后点击操作获取索引并从索引中获取数据

像这样

func addTagLabels() {
    for (index, tag) in hashtags.enumerated() {
        let button = UIButton(type: .system)
        button.tag = index //<--- Here assign index
        
       // Other code
    }
}
class CustomButton: UIButton {
    var data: String = ""
}

func addTagLabels() {
    for tag in hashtags {
        let button = CustomButton()
        button.setTitle("#\(tag)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
        button.data = tag //<-- Here
        // Other code
    }
}

@objc func didTap(_ sender: UIButton) {
    if let btn = sender as? CustomButton {
        delegate?.didTap(tag: btn.data)
    }
}

方式2

创建一个自定义UIButton子类并传递值

class CustomButton: UIButton {
    var data: String = ""
}

func addTagLabels() {
    for tag in hashtags {
        let button = CustomButton()
        button.setTitle("#\(tag)", for: .normal)
        button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
        button.data = tag //<-- Here
        // Other code
    }
}

@objc func didTap(_ sender: UIButton) {
    if let btn = sender as? CustomButton {
        delegate?.didTap(tag: btn.data)
    }
}

class自定义按钮:ui按钮{
变量数据:String=“”
}
func addTagLabels(){
用于hashtags中的标记{
let button=CustomButton()
setTitle(“#\(标记)”,表示:。正常)
addTarget(self,action:#选择器(didTap),for:.touchUpInside)
button.data=标记//