【问题标题】:Swift : Segue Taped menu item's name on navigation bar title with SWRevealViewControllerSwift : 使用 SWRevealViewController 在导航栏标题上 Segue Taped 菜单项的名称
【发布时间】:2015-12-09 17:57:00
【问题描述】:

我想将 SWRC 菜单上按下的项目的标题传递为 sw_front 导航栏的标题

所以两个 VC : 1 侧菜单“sw_rear” 2-前端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
}

【问题讨论】:

    标签: ios iphone swift segue swrevealviewcontroller


    【解决方案1】:

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

    【讨论】:

    • xcode 建议为 prepareForSegue 添加覆盖,然后它崩溃 > 致命错误:在展开可选值时意外发现 nil > 指向destinationVC.NavBar.topItem?.title = menu[indexPath.row]
    • 尝试类似var title = menu[indexPath.row] 然后将标题分配给目标标题
    • 我做了但没用请你看看dropbox.com/s/16wd7l8fjj6ulf2/TwoSWRE.zip?dl=0
    • 您忘记添加 IBOutlet 并连接到您的故事板 UITableView,因为我们需要它来处理这一行 var indexPath = self.mytableview.indexPathForCell(sender as! UITableViewCell)!
    • 所以设置断点`destinationVC.NavBar.topItem?.title = menu[indexPath.row]`看看数组中有没有值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多