如何从字符串数组中获取文本并将其用作按钮的文本?

如何从字符串数组中获取文本并将其用作按钮的文本?

问题描述:

我正在尝试获取具有以下信息的数组:[“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 
} 

而这里是接收的数据和应该代码操纵按钮显示所选[字符串]文:

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) 

} 
+0

设置WillAppear或DIdAppear在FirstVc按钮上的文字为驳回第二控制器后ViewWilApppear被称为firstVC –

在SET按钮标题中的问题,也如果selectedOccasion是你需要通过指数摆脱阵列文字传递给您的代理字符串数组

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) 

} 
+0

的太感谢你了!还有一个问题:我实现了你的代码,它在UIButton中显示文本。但它显示文本为[“日期”],而不是仅显示单词日期。任何想法如何解决这个问题? –

+0

您不应该在数组上使用'description'来将结果呈现给用户。 – rmaddy

+0

@ShaliniSingh检查更新 –