【问题标题】:How do I take the text from a array of strings and use it as the text for a button?如何从字符串数组中获取文本并将其用作按钮的文本?
【发布时间】:2017-08-23 22:50:00
【问题描述】:

我正在尝试获取包含以下信息的数组:["Concert", "Coachella", "Date"] 并将所选数组的文本转换为按钮的文本。因此,当我在数组中选择“日期”选项(显示在表格视图中)时,应该将该数据发送回我的第一个 VC,其中文本“日期”显示为按钮上的文本。由于某种原因,我似乎无法从 [String] 中提取字符串。提取该字符串后,我可以将该字符串设置为 UIButton 的标题文本。

这是表格视图显示选项的代码,用户选择一个选项并将数据发送到另一个 VC:

var delegate : SentDataBack?

@IBOutlet weak var occasionsTableView: UITableView!
@IBOutlet weak var occasionSearchBar: UISearchBar!

var addedOccasions = ["Coachella", "Concert", "Date"]

var index = 0

var selectedCell = [""]

override func viewDidLoad() {
    super.viewDidLoad()

    occasionsTableView.delegate = self
    occasionsTableView.dataSource = self

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    //let addedOccasions = ["Coachella", "Concert", "Date"]

    cell.textLabel?.text = addedOccasions[indexPath.row]

    cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1)

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return(addedOccasions.count)

}

// need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    index = indexPath.row

    selectedCell = [addedOccasions[index]]

}

@IBAction func doneButtonPressed(_ sender: Any) {

    delegate?.dataSentBack(occasionSentBack: selectedCell)
    // sends back selected option
    dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss
}

下面是接收该数据的代码,它应该操纵一个按钮来显示所选 [String] 的文本:

func dataSentBack(occasionSentBack: [String]) {

configureAddedOccasionButton(selectedOccasion: occasionSentBack)

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list
}

func configureAddedOccasionButton(selectedOccasion : [String]) {

    //set look of occasion button
    addedOccasion.isHidden = false
    addedOccasion.titleLabel?.text = selectedOccasion.description
    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43)
    buttonSelected(buttonPressed: addedOccasion)

}

【问题讨论】:

  • 在 FirstVc 中设置 WillAppear 或 DIdAppear 中的按钮文本,因为在关闭第二个控制器 ViewWilApppear 后调用 firstVC

标签: arrays swift string uibutton


【解决方案1】:

如果 selectedOccasion 是一个字符串数组,则 setButton 标题中的问题也需要通过索引从数组中获取文本传递给您的委托

 var delegate : SentDataBack?

    @IBOutlet weak var occasionsTableView: UITableView!
    @IBOutlet weak var occasionSearchBar: UISearchBar!

    var addedOccasions = ["Coachella", "Concert", "Date"]

    var index = 0

    var selectedCell = ""


    override func viewDidLoad() {
        super.viewDidLoad()

        occasionsTableView.delegate = self
        occasionsTableView.dataSource = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        //let addedOccasions = ["Coachella", "Concert", "Date"]

        cell.textLabel?.text = addedOccasions[indexPath.row]

        cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1)

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return(addedOccasions.count)

    }

    // need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        index = indexPath.row

        selectedCell = addedOccasions[index]

    }

    @IBAction func doneButtonPressed(_ sender: Any) {

        delegate?.dataSentBack(occasionSentBack: selectedCell)
        // sends back selected option
        dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss
    }

//////////

func dataSentBack(occasionSentBack: String) {

    configureAddedOccasionButton(selectedOccasion: occasionSentBack)

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list
}

func configureAddedOccasionButton(selectedOccasion : String) {

    //set look of occasion button
    addedOccasion.isHidden = false
    addedOccasion.setTitle(selectedOccasion,for: .normal)

    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43)
    buttonSelected(buttonPressed: addedOccasion)

}

【讨论】:

  • 非常感谢!还有一个问题:我已经实现了您的代码,它正在 UIButton 中显示文本。但是,它将文本显示为 ["Date"] 而不仅仅是显示单词 Date。知道如何解决这个问题吗?
  • 您不应该在数组上使用description 来将结果呈现给用户。
  • @rmaddy 我会使用什么而不是描述?
  • 这完全取决于您希望如何为用户格式化数据。如果您想要一个简单的逗号(或其他分隔符)分隔的字符串列表,请使用 Arrayjoined(separator:) 方法。
  • @ShaliniSingh 您将始终将一个字符串传递回 vc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多