坑爹的導航欄返回鍵(navigationItem.backButtonBarItem)

weixin_34006468發表於2015-07-20

如何自定義 navigation controller 的 pop 轉場動畫?

今天要自定義從 ViewController A 到 ViewControllerB 的轉場( segue )動畫,這兩個 controller 都在一個 UInavigationController 管理之下。有 A push 到 B 的專場做好了,但是發現怎麼都攔截不到點選系統自帶導航欄上返回鍵的事件來 prepare 我自己寫的 unwind segue。Google 了半個小時,找到了一個網頁,標題跟我的問題一樣一樣的,好開心啊。結果點進去就黑線了,所有的評論都說 build-in back 事件是攔截不到的...

“I've already wasted so much time on this that I am almost tempted to
go down the route of subclassing UINavigationController, trying to
trigger a segue manually on a pop.”

”Oh. So your question is not actually about segue unwinding. That's disappointing. :)“

“If you want a button in the UINavigationBar to do something other than "go back", don't use a back button.”

好吧,那按照大家說的把 back button 換掉。StackOverFlow 都說這麼幹,

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
self.navigationItem.backButtonBarItem = backButtonItem;

結果怎麼都替換不掉 build-in back button,也沒有的任何警告。試來試去,又半小時沒了。

解決方案

正確的姿勢是,將 build-in back 隱藏掉,然後新增一個 leftBarButtonItem,問題解決。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Library" style:UIBarButtonItemStyleDone target:self action:@selector(onLibraryButtonClicked:)]];
    self.navigationItem.hidesBackButton = YES;
}


- (IBAction)onLibraryButtonClicked:(id)sender {
    [self performSegueWithIdentifier:@"segueUnwindFromMapToCollection" sender:self];
}

The Life Cycle of a Segue

好在折騰了這麼久也對 segue 的 life time 有了更深的瞭解。Apple官方文件如下,

To understand how custom segues work, you need to understand the life cycle of a segue object. Segue objects are instances of UIStoryboardSegue or one of its subclasses. Your app never creates segue objects directly; they are always created on your behalf by iOS when a segue is triggered. Here’s what happens:

  1. The destination controller is created and initialized.
  • The segue object is created and its initWithIdentifier:source:destination: method is called. The identifier is the unique string you provided for the segue in Interface Builder, and the two other parameters represent the two controller objects in the transition.
  • The source view controller’s prepareForSegue:sender: method is called. See Configuring the Destination Controller When a Segue is Triggered.
  • The segue object’s perform method is called. This method performs a transition to bring the destination view controller on-screen.
  • The reference to the segue object is released, causing it to be deallocated.

另外得到的兩個結論:

  1. custom segue perform 方法中,destinationViewController 的 viewDidLoad: 方法是在其 view 被加到 view tree 上之後被呼叫的。(通常在做動畫時,會先將 destinationViewController.view 作為 subView 加在前一個 sourcdViewController.view 上去。)
  2. custom segue perform 方法中,viewWillAppear: 是在 destinationViewController 被 push 之後呼叫的。

歡迎來我的個站逛逛: http://alexyu.me/

相關文章