在用 UIBarButtonItem 的時候,要用 custom view, 發現加 target 和 action 都沒有用,無法觸發 action 所設定的 SEL
物件。
使用CustomView
UIBarButtonItem *item = [UIBarButtonItem initWithCustomView:customView];
item.target = self;
item.action = @selector(didSelectItem:);
self.navigationitem.leftbarbuttonitem = item
這時候 selector didSelectItem:
並不會如期被呼叫到。
需自己處理
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
UIKit Framework Reference > UIBarButtonItem Class Reference >
- initWithCustomView:
在文件裡面提到,當使用這個 method 來初始化一個 UIBarButtonItem
的實例時,他們則期望開發者應該要自己處理使用者的互動,