objective c - Pop controller after back bar button is pressed -


i have uinavigationcontroller ans chain of 3 simple controllers. each 1 has button. when press button next controller pushed. viewcontroller1 -> viewcontroller2 -> viewcontroller3. when push button on 3rd view want move first view. using of backbarbuttonitem obligatory. here code second controller:

#import "viewcontroller2.h"  static bool isbackbuttonpressed;  @implementation viewcontroller2  - (void)viewdidload {     self.navigationitem.backbarbuttonitem = [[uibarbuttonitem alloc]initwithtitle:@"back 3" style:uibarbuttonitemstylebordered target:nil action:nil];     [super viewdidload]; }  - (void)viewwillappear:(bool)animated {     if (isbackbuttonpressed) {         [self.navigationcontroller popviewcontrolleranimated:yes];     } else {         isbackbuttonpressed = yes;     }     [super viewwillappear:animated]; }  @end 

but when press button on third view return second view instead of first view. me return first view pressing button on third view. tried suggestions answers don't help.

  1. adding selector backbarbuttonitem doesn't because never called.
  2. adding [self.navigationcontroller poptorootviewcontrolleranimated:yes] in viewwilldisappear methos doesn't work. don't know why. think actual problem how backbarbuttonitem works. other suggestions?

the behaviour try achieve exists in calendar on iphone. when rotate iphone landscape weeek view. go event details, , rotate portrait. when press button day view not week view, controller weekview skipped.

after countless number of tries solution not use backbarbuttonitem! whatever goes previous viewcontroller instead of calling selector

instead use leftbarbuttonitem navigation, guarantees calling action.

here example

uibutton *backbutton = [[uibutton alloc] initwithframe:cgrectmake(0, 0, 27, 22)]; [backbutton setimage:[uiimage imagenamed:@"backbutton"] forstate:uicontrolstatenormal];     [backbutton addtarget:self action:@selector(backbuttonpressed:) forcontrolevents:uicontroleventtouchupinside];    self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview:backbutton]; 

this calls backbuttonpressed action.. works both ios 6 , 7


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -