2015-02-08 64 views
1

我試圖自定義外觀UIBarButtonItemUISplitViewController分爲兩個UINavigationController的(細節和主視圖),但保持擊中磚牆。我首先按照建議here,但一直無法達到預期的效果。UISplitViewController中的自定義UIBarButtonItem不響應點擊(iPhone)

如果,如果我實現我的詳細視圖控制器以這種方式viewDidLoad方法的返回按鈕:

if let svc = splitViewController { 
    navigationItem.leftBarButtonItem = svc.displayModeButtonItem() 
} 

我得到默認的「後退」按鈕導航回母版視圖控制器。但是,如果我嘗試使用以下代碼自定義按鈕:

if let svc = splitViewController { 
     let searchButton = UIBarButtonItem(
      image: UIImage(named: "magnifying-glass"), 
      style: UIBarButtonItemStyle.Plain, 
      target: svc.displayModeButtonItem().target, 
      action: svc.displayModeButtonItem().action 
     ) 

     navigationItem.leftBarButtonItem = searchButton 
    } 

我得到了導航欄所需的圖標,但點擊時,它什麼都不做。我已經在控制檯中調試了操作和目標,並且操作都是「_triggerDisplayModeAction:」,而控制器是UISplitViewController的一個實例。

關於這裏發生了什麼的任何想法?

+0

是UISplitViewController的實例相同嗎? – 2015-02-10 19:21:59

+0

相同的內存地址 – kellanburket 2015-02-10 19:51:24

+0

你可以共享代碼裏面displayModeButtonItem()方法 – 2015-02-10 19:54:45

回答

1

我建議你在主視圖控制器的prepareForSeque中創建和使用自定義的UIBarButtonItem。分割視圖控制器默認會創建你的詳細視圖控制器的新實例!忽略與iPhone ios7support的部分,這是另一回事:-) https://github.com/op183/MasterDetailDemo

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showDetail" { 
     if let indexPath = self.tableView.indexPathForSelectedRow() { 
      let object = objects[indexPath.row] as String 
      if let navController = (segue.destinationViewController as? UINavigationController) { 
       let controller = navController.topViewController as DetailViewController 
       controller.detailItem = object 
       let defaultBarButton = splitViewController?.displayModeButtonItem() 
       let searchButton = UIBarButtonItem(
        image: UIImage(named: "magnifying-glass"), 
        style: UIBarButtonItemStyle.Plain, 
        target: defaultBarButton.target, 
        action: defaultBarButton.action 
       ) 
       controller.navigationItem.leftBarButtonItem = searchButton 
       controller.navigationItem.leftItemsSupplementBackButton = true 
      } else { 

       // iPhone ios7support 
       (segue.destinationViewController as DetailViewController).detailItem = object 
      } 
     } 
    } 
} 
+0

感謝關於拆分視圖控制器行爲的見解,但在進行您提出的調整後,我仍然遇到同樣的問題。按鈕在那裏,但不會鏈接回去 – kellanburket 2015-02-13 23:40:00

+0

讓我們通過github分享你的代碼,如果你想的話。這對我來說很有趣,因爲我經常使用'custom'UIBarButtonItem,看起來,你的代碼片段似乎很好。 – user3441734 2015-02-14 06:58:49

+0

這是在這裏設置:https://github.com/kellanburket/ravelry/blob/master/ravelry/LoaderImageController.swift – kellanburket 2015-02-14 18:56:17

相關問題