Ios 如何从表格静态单元格更改按钮标题?
我有一个HomeViewController,它以模态方式连接到一个标识符为:pickSubjectAction的导航控制器 在主题上,PickerTableViewController是我选择主题的地方。这是我的密码Ios 如何从表格静态单元格更改按钮标题?,ios,swift,xcode,swift3,ios10,Ios,Swift,Xcode,Swift3,Ios10,我有一个HomeViewController,它以模态方式连接到一个标识符为:pickSubjectAction的导航控制器 在主题上,PickerTableViewController是我选择主题的地方。这是我的密码 import UIKit class SubjectPickerTableViewController: UITableViewController { var subjects:[String] = [ "English", "Ma
import UIKit
class SubjectPickerTableViewController: UITableViewController {
var subjects:[String] = [
"English",
"Math",
"Science",
"Geology",
"Physics",
"History"]
var selectedSubject:String? {
didSet {
if let subject = selectedSubject {
selectedSubjectIndex = subjects.index(of: subject)!
}
}
}
var selectedSubjectIndex:Int?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return subjects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SubCell", for: indexPath)
cell.textLabel?.text = subjects[indexPath.row]
if indexPath.row == selectedSubjectIndex {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
//Other row is selected - need to deselect it
if let index = selectedSubjectIndex {
let cell = tableView.cellForRow(at: IndexPath(row: index, section: 0))
cell?.accessoryType = .none
}
selectedSubject = subjects[indexPath.row]
//update the checkmark for the current row
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "saveSelectedSubject" {
if let cell = sender as? UITableViewCell {
let indexPath = tableView.indexPath(for: cell)
if let index = indexPath?.row {
selectedSubject = subjects[index]
}
}
}
}
}
这还与标识符savedSelectedSubject分隔
问题1:如何从按钮切换到tableview控制器?
我试过了,但失败了
问题2:如何更改标题为“选定主题”的按钮
我的资源:
感谢您的帮助。谢谢
如何从按钮切换到tableview控制器?我试过了
这是一次失败
performsguewithidentifier
在以Any
格式发送时需要字符串
格式。调用此函数的正确方法是
self.performSegue(withIdentifier: "pickSubjectAction", sender: self)
如何更改标题为“来自选定主题”的按钮
我猜您希望在控制器中反映所选主题,该控制器显示在savedSelectedSubject
segue之后。让ViewController A成为您推动/展示的对象,ViewController B成为推动/展示的对象。请按照以下步骤操作:
segue.destination
属性从prepare(对于segue:UIStoryboardSegue,sender:Any?)获取目标控制器(B)李>
在B中创建一个公共变量,您可以在其中设置所选主题。
使用该属性可以显示选定的主题标题
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let B = segue.destination
// Assuming B's public var is named selectedSubject and is of String type
if let subject = self.selectedSubject {
B.selectedSubject = subject
}
}
在控制器B中
override func viewWillAppear() {
super.viewWillAppear()
button.setTitle(self.selectedSubject, for: .normal)
}
// We already have the value of selectedSubject which we are using to set title for the button.
Q1:pickSubjectAction是一个字符串
?如果是这样的话,请在其周围加上双引号。问题2:您可以在didSelect
之后保存标题字符串,然后将其加载到视图中的按钮标题将出现pickSubjectAction是一个序列标识符。@antonio081014,感谢第一步起作用。你能给我看一点代码如何做你的第二步吗。您可以有几个选项,一个是UserDefaults,另一个是Notification。第一个选项更容易实现。这是我忘记报价的感谢信。根据我的代码,你能解释更多的第二步吗。按钮在哪里。设置标题?@JohnDoe我添加了第三步,您可以通过该步骤设置按钮标题。如果您有任何其他疑问,请告诉我!