【发布时间】: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