marrioa
marrioa

Reputation: 1275

Swift : Segue Taped menu item's name on navigation bar title with SWRevealViewController

I want to pass the title of pressed item on SWRC menu to be the title of navigation bar of sw_front

So two VC : 1- side Menu "sw_rear" 2- front VC "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

Upvotes: 1

Views: 460

Answers (1)

Lamour
Lamour

Reputation: 3030

The prepareForSegue method has to be implemented outside of the cellForRowAtIndexPath method, the array should be filled inside of the life cycle..

  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]
        }
    }
}

Upvotes: 2

Related Questions