Apply back action to uiButton - objective-c

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?

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];
}

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

If I did not understand wrong, you need this;
[self.navigationController popViewControllerAnimated:YES];

Related

iOS: dismiss a modal and push a new viewcontroller on the main NavigationController

Here is my problem (See illustration bellow):
I have a main screen (MainViewController) from which i present a form modal (ModalController) embedded in a Navigation controller.
After completing the form I want to present the result to the user and dismiss the modal.
The result is shown with a ItemViewController and it should be pushed on the main stack, i.e. in a navigation controller and if the user presses back, he returns to the main screen.
My problem is how to dismiss the modal and push the new view controller at the same time?
What I tried:
dismiss the modal and push the new view controller in the completion block using
self.parentViewController.navigationController pushViewController:itemViewController but only the modal is dismissed.
push the view controller the same way then dismiss, also without effect.
unwind the modal to the main screen and from the unwindSegue method, instantiate and push the new view controller. Unfortunately, the code below has the same effect..
- (IBAction)unwindSegue:(UIStoryboardSegue *)segue {
ItemViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ItemViewController"];
[self.navigationController pushViewController:viewController animated:YES];
}
Might be important
In the storyboard, ItemViewController is embedded in a NavigationController since it is defined as on the picture below:
Maybe this can help:
in viewDidLoad method of ItemViewController:
UIImage *backButtonImage = [UIImage imageNamed:#"back.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 55, 45);
[backButton addTarget:self
action:#selector(goHome)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
- (void)goHome{
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];
}

How can i add a custom uibarbuttonItem to my navigation item in all view controllers?

I am trying to add a custom UIBarButtonItem to my navigationItem. This button will be available in all my view controllers. so instead of adding it to each viewDidLoad method in each viewDidLoad I am trying to add it in my application Delegate class in a seperate method that i will call in the viewDidLoad method.
Here is my Code:
UIImage* image = [UIImage imageNamed:#"some-header-icon.png"];
CGRect frame = CGRectMake(0,0,image.size.width,image.size.height);
UIButton* hellBtn = [[UIButton alloc] initWithFrame:frame];
[hellBtn setBackgroundImage:image forState:UIControlStateNormal];
[hellBtn setShowsTouchWhenHighlighted:NO];
[hellBtn addTarget:self action:#selector(goToHell) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:hellBtn];
[self.nav.navigationController.navigationItem setRightBarButtonItem:rightBarButtonItem];
[hellBtn release];
[rightBarButtonItem release];
if I replace the same code blog in any viewDidLoad of any viewController and change
[self.nav.navigationController.navigationItem setRightBarButtonItem:rightBarButtonItem];
By:
[self.navigationItem setRightBarButtonItem:rightBarButtonItem];
It works perfectly.
Any Idea Why?
Create a subclass of UIViewController for ex. UIViewControllerBase, override viewDidLoad method and inside it create your rightBarButtonItem and set it. Then make all of your controllers inherit UIViewControllerBase - simple OOP scenario.
You should not use this self.nav.navigationController since nav is a UINavigationController then dont get the .navigationController
Just use [self.navigationItem setRightBarButtonItem:rightBarButtonItem];

Create UIButton inside Objective C method

I am trying to create a UIButton in code, and this code is in some methods which will get called from some class.
Here is the method that creates the button
-(void)createButton
{
NSLog(#"createButton");
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Get Friends" forState:UIControlStateNormal];
[button setFrame:CGRectMake(100, 100, 100, 50)];
[button addTarget:self action:#selector(loadTableView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
but it never appears in the view, what's wrong?
Edit: if I call this method from viewDidLoad then it works!
Edit 2: the method is in the ViewController class and I call it from MyFacebooDelegate class
here is the call code from MyFacebooDelegate class:
ViewController *m2 = [[ViewController alloc] init];
[m2 createButton];
When you create a new ViewController using ViewController *m2 = [[ViewController alloc] init]; it is not the same ViewController that is handling the screen.
Instead of allocating a ViewController, you should be using the one that's created when the application starts.
May be your view is not loaded from Nib yet at the moment. If you created view by instantiating
[[UIViewController alloc] initWithNibName:#"someNibName" bundle:nil];
than view controller will be created and start to load view from Nib asynchronously. So, if UIViewController is instantiated, that does not mean UIView is. So, that's why your button work when created from -viewDidLoad: callback.

Click on button to change to table view in Objective-c

I have two button in my program(figure A), I want to change to a table view(figure B) when I click on one button. Can someone please advise how to do that?
Thanks
Quite simple actually. In the button's handler method, create the table view controller and display it modally or push it on the navigation stack.
[myButton addTarget:self action:#selector(displayTableView:) forControlEvents:UIControlEventTouchUpInside];
-(void)displayTableView:(id)sender
{
MyTableViewController* myTVC = [[MyTableViewController alloc] initWithNibName:#"myTVC" bundle:nil];
[self.navigationController presentModalViewController:myTVC animated:YES];
//[self.navigationController pushViewController:myTVC animated:YES]; //2nd option
[myTVC release];
}

Adding Custom Back Button on NavigationBar UIButton buttonWithTypw:101 not working

I had used the below mention code for a custom left pointed back button and it was working properly..
but after a few days, it changed back to a rectangular button..
can anyone help me to get it back to its original type....
Thanks...
UIButton *backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
UIBarButtonItem* newBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
graphSecondViewCtrl.navigationItem.leftBarButtonItem = newBackButton;
Button type 101 is not defined in the SDK, and can stop working at any OS upgrade.
I'd use a buttonWithType:UIButtonTypeCustom and then add an image that gives you the look you need by calling setImage:forState on the custom button object.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"backBarButtonNormal.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:#"backBarButtonHighlighted.png"] forState:UIControlStateHighlighted];
// Rest of the code is the same.
The real question is why you'd need to recreate the standard button that the navigation controller automatically gives you in the first place. Make sure you work with the tools, and not against them.
If you need to track when the view controller gets swapped in and out, you can set your view controller to be the delegate of the navigation controller like this:
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setDelegate:self];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController != self)
NSLog(#"We're going away...");
}
- (void)viewDidUnload {
[super viewDidUnload];
[[self navigationController] setDelegate:nil];
}
The button type 101 is undocumented. It is not something Apple has agreed to maintain so it can change any time. It is incorrect to rely on such stuff. You should consider a custom button with images to be sure that it will not break like it has for you now.