问题
I have HomeViewController that's segued modally to a Navigation Controller with an identifier of: pickSubjectAction
And on SubjectPickerTableViewController is where my subjects to choose. This is my code
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]
                }
            }
        }
    }
}
This is segued also with identifier: savedSelectedSubject.
Q1: How can i segued from button to the tableview controller? I tried this but failed Q2: How to changed the button titled from selected Subject?
my resources: https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
Any help is appreciated. Thanks
回答1:
How can i segued from button to the tableview controller? I tried this but failed
performSegueWithIdentifier expects a String format while you are sending it in Any format.  Correct way for call this function is 
self.performSegue(withIdentifier: "pickSubjectAction", sender: self)
How to changed the button titled from selected Subject?
I guess you want to reflect the selected subject in the controller presented after savedSelectedSubject segue. Let ViewController A be the one through which you are pushing/presenting and ViewController B be the one which is pushed/presented. Follow the following steps :
- For this you need to fetch the destination controller (B) from prepare(for segue: UIStoryboardSegue, sender: Any?)function viasegue.destinationproperty.
- Make a public variable in B in which you can set your selected subject. Using that property you can show your selected subject title. - 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 } }
- In Controller 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.
来源:https://stackoverflow.com/questions/42753291/how-to-change-button-title-from-table-static-cells