2015-09-13 1 views
1

Я хочу передать название прессованного пункта меню SWRC быть заголовком панели навигации sw_frontSwift: SEGUE имени ТЕСЬМЫ пункта меню на названии навигации бара с SWRevealViewController

Так два VC: 1 сторона меню "sw_rear" 2- передняя ВК "sw_front"

class menuVC: UITableViewController { 

var menu = ["Title1","Title2","Title3"] 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

    // Configure the cell... 

    cell.textLabel?.text = menu[indexPath.row] 

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "Pushitem" 
     { 
      if let destinationVC = segue.destinationViewController as? frontVC{ 
       destinationVC.NavBar.topItem?.title = menu[indexPath.row] 
      } 
     } 
    } 

    return cell 
} 

enter image description here

enter image description here

ответ

2

prepareForSegue метод должен быть реализован за пределами метода cellForRowAtIndexPath, массив должен быть заполнен внутри жизненного цикла ..

var menu = [String]() //<---- declare the array Globally 

@IBOutlet weak var mytableview:UITableView!  //<---- connect this outlet to your tableview 

override viewDidLoad() 
{ 
    super.viewDidLoad() 
menu = ["Pathology","Biochem","Physiology"] //<----- fill the array into the life cycle 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 
cell.textLabel?.text = menu[indexPath.row] 
    return cell 
} 


func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "Pushitem" 
    { 
     if let destinationVC = segue.destinationViewController as? frontVC{ 
      var indexPath = self.mytableview.indexPathForCell(sender as! UITableViewCell)! //<---- you could get the indexPath of the cell 
      destinationVC.NavBar.topItem?.title = menu[indexPath.row] 
     } 
    } 
} 
+0

Xcode предложить добавить переопределение prepareForSegue то после этого он выходит из строя> Фатальная ошибка: неожиданно обнаружили, nil во время разворачивания необязательного значения>, указываемого на destinationVC.NavBar.topItem? .title = menu [indexPath.row] – marrioa

+0

попробуйте что-то вроде 'var title = menu [indexPath.row]', затем назначьте заголовок целевому заголовку – Lamar

+0

Я сделал, но didn Не работайте, пожалуйста, если вы посмотрите https://www.dropbox.com/s/16wd7l8fjj6ulf2/TwoSWRE.zip?dl=0 – marrioa

Смежные вопросы