var = string [indexpath.row],Prepareforsegue,调用字符串作为导航栏标题(swift)

时间:2015-07-24 21:01:11

标签: ios swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "packtofeedsegue"){
        let indexPath = self.tableView.indexPathForSelectedRow()
        var lastviewed = userids[indexPath!.row]
        println(lastviewed)
    }
}

第二个视图控制器:

var lastviewed = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController.navigationBar.topItem.title = lastviewed
}

我正在尝试preparesegue将变量设置为行文本,然后在下一个视图控制器中调用该文本作为导航栏的标题。

我得到:Cannot assign value of type [(String)] to a value of String? for self.navigationController.navigationBar.topItem.title = lastviewed

当我将var lastviewed = [String]()更改为var lastviewed = String()时,没有任何错误,但没有显示任何错误:println显示正确的文字,因此正确的信息将进入prepareforsegue

1 个答案:

答案 0 :(得分:1)

[String]表示“String类型的对象数组”。你想要的是String

var lastViewed = ""

将标题配置放入viewWillAppear。它应该足够了

self.title = lastViewed

分配给prepareForSegue中的视图控制器:

let destination = segue.destinationViewController as! SecondController
destination.lastViewed = userIds[indexPath!.row]!