1

I am new in ios development and I want to add back button in my app. For this I write code in -(view)didLoad:

UIButton *btnbarBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnbarBack.frame = CGRectMake(0, 0, 30, 22);
[btnbarBack setImage:[UIImage imageNamed:@"arrow_left.png"] forState:UIControlStateNormal];
[btnbarBack addTarget:self action:@selector(OnClick_btnBack:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *btnbarBack_1 = [[UIBarButtonItem alloc] initWithCustomView:btnbarBack];

self.navigationItem.rightBarButtonItem = btnbarBack_1;

and I use popViewController to go back Clickevent code is :

-(IBAction)OnClick_btnBack:(id)sender  {
    [self.navigationController popViewControllerAnimated:YES];
}

But when I click on button event fires but no change appear!

I already tried:

-[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];

-[self.parentViewController.navigationController popToRootViewControllerAnimated:YES];

-[self dismissViewControllerAnimated:YES completion:nil];

But these 3 also not working. Is there any other option to solve this problem.

Yuchen
  • 30,852
  • 26
  • 164
  • 234
Vidhi
  • 397
  • 1
  • 5
  • 17
  • 2
    Saying something "doesn't work" provides no information what so ever. Explain what _is_ happening when you press the button. Is the button not showing? Is it going to the wrong controller? Is it happening but it's not animated? – Gary Riches Feb 21 '15 at 08:48
  • I edited my question,plz refer that..I want to just clarify that event fires,that I already checked but code is not working – Vidhi Feb 21 '15 at 08:54
  • Please check the viewController's hierarchy. – Zigii Wong Feb 21 '15 at 08:56
  • Yea, viewController hierarchy is correct..I just checked – Vidhi Feb 21 '15 at 09:13
  • If the method is getting called NSLog the navigation controller: NSLog("%@", self.navigationController); – Gary Riches Feb 21 '15 at 09:15
  • Can you check the self.navigationController.viewcontrollers? – Roy Feb 21 '15 at 09:16
  • First of all, `self.naviagtionController.rightBarButtonItem` is `UIBarButtonItem` type. However, you are assigning a `UIButton` to it. This is not proper already. Take a look at this one. http://stackoverflow.com/questions/16451639/custom-uibarbuttonitems-from-uibuttons-with-custom-images-is-it-possible-to-ma – Yuchen Feb 21 '15 at 23:45
  • @YuchenZhong: I think there is not any problem with UIBarbuttonItem & UIButton, because button shows at front side & click event fires but doesn't go to previous screen..Here I clarify that I tried UIBarButtonItem as back button but still It doesn't work..Please give me another solution..Thank you in advance – Vidhi Feb 25 '15 at 12:00
  • Which method to use to navigate to the previous screen is really depending on how the view is created. So, could you shows us code about how you enter the current view? If you are using storyboard instead of code, could you simply take a screenshot of that or explain what segue you are using? Also, could you add a NSLog() in your `OnClick_btnBack` just to make sure the method is being called when you tap on the button. – Yuchen Feb 25 '15 at 14:07
  • @YuchenZhong:yea..I had put alert ,and checked, but clickevent fires,It is worked perfect ,there is not any storyboard or xib file,only .h & .m files are there..actually It is existing app,and I have to do some changes in it..plz help.. – Vidhi Feb 26 '15 at 06:10

1 Answers1

0

It looks like you're recreating a button that is already in your storyboard. If it isn't, you have an IBAction connected to something in the storyboard. Change the "IBAction" in the parentheses to "void" and that should fix it.

Also...why not create Uibarbutton with a custom method and set the image in there?

kygcoleman
  • 734
  • 15
  • 25
  • ya..I have changed IBAction to void but not found any changes..back button doesn't work..and also tried UIbarbutton but doesn't effect anything..click event fires,but doesn't go previous screen...Is there any other solution???plz help – Vidhi Feb 25 '15 at 11:51
  • Do you have a storyboard? – kygcoleman Feb 25 '15 at 14:32
  • No there is not any storyboard or xib ,only .h and .m files are there...I have one existing app and I have to do some changes in it.and put back button right side in navigation controller is one of them.... – Vidhi Feb 26 '15 at 06:13
  • @VidhiTrivedi interesting way to build an app. I think storyboards are so easy to use! Especially in a situation like this. – kygcoleman Feb 26 '15 at 06:15
  • You are absolutely right ,but In existing app I can't use that,plz give another solution..totally frustrated with this small-big problem..help me..Thx in advance – Vidhi Feb 26 '15 at 06:23
  • How you tried to `po self.navigationController.viewControllers` in the debug **OR** `NSLog(@"%@", self.navigationController.viewControllers);`? To see what view controllers are in the navigationController's stack? @VidhiTrivedi – kygcoleman Feb 26 '15 at 06:40
  • You also said this was an existing app. Which part did you change? @VidhiTrivedi – kygcoleman Feb 26 '15 at 14:25
  • In my existing app, in screen, left side - left navigationcontroller(side menu) is there -it works perfectly and I want to add Back button at right side(This is change ) and I suffer problem with that as you know.. – Vidhi Feb 27 '15 at 11:17