TableViewController (NavigationController に埋め込まれている) から別の TableViewController へのセグエを (ストーリーボード経由で) 実行しています。つまり、セルを選択し、選択したセルのテキストを次のビューのタイトルとして表示したい別の TableView を提示します。
私はこれを達成していますが、100%正しくはありません。セルの最初の初期選択では、navigationItem タイトルが設定されていません。同じセルを戻って次に進むと、タイトルが設定されます。
最初のスニペットは、選択したセルのタイトルで destinationViewControllers 変数を設定しているセルを選択している最初の viewController です。
var valueToPass:String?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!;
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!;
valueToPass = currentCell.textLabel!.text
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "tripSegue") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destinationViewController as! TripTableViewController
// setting the view controllers property that will store the passed value
viewController.passedValue = valueToPass
}
}
2 番目のスニペットは、navigationItem タイトルを設定する destinationViewController からのものです。
var passedValue: String?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = passedValue
}