Dancer
Dancer

Reputation: 17651

Apply back action to uiButton

I need to have a UIButton (with a class of RtnBtn) which basically replicates the back button action used in the navigation bar action at the end of a process - I'm fairly new to IOS dev and I'm not sure how to approach this - should it be done via a push or is there a better option to apply coded action directly to the button?

Upvotes: 1

Views: 930

Answers (3)

Vishwa Patel
Vishwa Patel

Reputation: 484

Can you post some code here, what you have done?, you can add custom back button by below code,

UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonBack setBackgroundImage:YOURImage forState:UIControlStateNormal];
[buttonBack addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:buttonBack] autorelease];
self.navigationItem.leftBarButtonItem = backButton;

Method to handle event,

 -(void)backPressed
 {
   [self.navigationController popViewControllerAnimated:YES];
 }

Upvotes: 1

caglar
caglar

Reputation: 1069

If I did not understand wrong, you need this;

    [self.navigationController popViewControllerAnimated:YES];

Upvotes: 1

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

You need to create the button, specify a selector as it's action, and in that method call popViewController:animated: on your navigation controller.

Upvotes: 1

Related Questions