Use another button to do the same thing as the back button - ios

What I have now?
I have two view controllers (VCA & VCB) and a navigation controller. VCA can push to VCB and VCB can go back to VCA by pressing back button in the navigation bar of VCB.
What I want to do?
I want to add another button in VCB to do the same thing as the back button in navigation bar does, that is when that button is pressed, I should be lead back to VCA, how can I do it?
What I have tried?
I tried to add a button in VCB and controller drag a line from the button to VCA so that they are connected, but I found that VCA's viewDidLoad method is called each time I back from VCB, this is not what I wanted.
Thanks!

You can realize some button (object of UIButton class), for example like this:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton.frame = CGRectMake(60.0f, 40.0f, 200.0f, 50.0f);
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:28.0f];
backButton.titleLabel.shadowOffset = CGSizeMake (0.0f, 1.0f);
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
And also you must to define corresponding selector for action of the button:
- (void)goBack {
[self.navigationController popViewControllerAnimated:YES];
}
Now, if you click this button you will come to previous view controller.

The action that is fired when you press a back button to pop a ViewController is:
popViewControllerAnimated:(BOOL)animated
So what you can do it to create a button and add that action to it:
UIButton *goBackButton = [[UIButton alloc] initWithFrame:CGRectMake(0,64,50,50)];
[goBackButton addTarget:self action:#selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goBackButton];
Now you actually have to implement that goBackAction:
-(IBAction)goBackAction:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
You could actually add the popViewControllerAnimated action to the button, like:
[goBackButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
The problem is that you are not able to pass that animated parameter to the method which will result in it using some junk that happens to be where that parameter was expected to be in memory. The result will be the pop sometimes being animated and other times not.

Related

Custom bar button item not performing segue

I have a right bar button item as defined in my viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];
}
I had linked the bar button item and performed segue with identifier but it's not pushing the view. Why is that so?
- (IBAction)btnShowHelp:(id)sender {
[self performSegueWithIdentifier:#"showHelp" sender:self];
}
delete the link between your button and - (IBAction)btnShowHelp:(id)sender, change your method to - (void)btnShowHelp(id)sender, then change your code in viewDidLoad like this :
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[helpButton addTarget:self action:#selector(btnShowHelp:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];
Turns out that btnShowHelp that was in my storyboard was already replaced by the new UIButton defined in viewDidLoad and there was no need for it.
https://developer.apple.com/reference/uikit/uibarbuttonitem/1617151-initwithcustomview?language=objc
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
I was doing [self.navigationItem.rightBarButtonItem setAction:] which I then realised I was suppose to set the action for my UIButton instead of the bar button item...
UIButton *helpButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[helpButton addTarget:self action:#selector(showHelp) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:helpButton ];
- (void)showHelp {
[self performSegueWithIdentifier:#"showHelp" sender:self];
}
Adding action and target to a custom Navigation BarButtonItem?

Adding button programmatically to perform segue

I am trying to create a button 'on the fly' using code, and i want that button to perform
segue to a different controller. As far as i know, creating segue programmatically is not possible, but how can i get pass that if the created button is not on my storyboard?
This is the code for creating the button:
- (void)addButton:(UIView*)view inLocation:(CGPoint)point
{
UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame= CGRectMake(point.x, point.y,30,30);
[but setTitle:#"CHOOSE" forState:UIControlStateNormal];
// The nilSymbol should call a method that will perform the segue
[but addTarget:self action:#selector(nilSymbol) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:but];
}
Any options to do that?
In Interface Builder, create a segue.
Since you can't connect the segue to the button (it doesn't exist yet), simply connect the segue to the starting ViewController.
Then, when you create the button:
// ....
[but addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....
And in the someMethod:
-(void)someMethod
{
[self performSegueWithIdentifier:#"segueIdentifier" sender:self];
}

Ios How to a add a button to my TabBarController then trigger the presentation of a modal view from it

I have a UITabBarController bassed app. I'm instantiating it from the app delegate and adding a custom button in the tab bar controller. when that button is clicked, I want to present another view modally, but I cant seem to figure out how to do it. to add the button I'm basically doing this
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.tabBarController.view addSubview:button];
[button addTarget:self action:#selector(showModalViewController:) forControlEvents:UIControlEventTouchUpInside];
and also in the app delegate I have a method
- (void) showModalViewController {
DummyViewController *addController = [[DummyViewController alloc]
initWithNibName:#"DummyViewController" bundle:nil];
//addController.delegate = self;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
// Create the navigation controller and present it modally.
[self.tabBarController.selectedViewController presentModalViewController:addController animated:YES];
// The navigation controller is now owned by the current view controller
}
I keep getting unrecognized seletor
Your selector looks for a method named showModalViewController: but your actual method is named showModalViewController. Change one or the other.
Either change the selector to #selector(showModalViewController) to match the existing method, or change the method to:
- (void)showModalViewController:(UIButton *)button {
Don't change both.

how to raise modal view from tab bar button

I'm using this:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
Anyone know how to get a modal view raised from say..the camera button (if the camera button were to be used for something else other than the camera)?
In BaseViewController.m the centre button is added in
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
Simply add the observer:
[button addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
And then obviously implement:
-(void)click:(id)sender{
[self presentModalViewController:[[UIViewController alloc] init] animated:YES];
}

send a message to a parent object form popover

my iOS application use storyboard
it have 2 view controllers:
- main storyboard view controller
- and popover view controller with some objects in it
i've got a button on main view controller and it creates programing every time i run the application:
*CGRect buttonFrame = CGRectMake(10., 10., 120., 50.);
oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[oneButton setImage:[UIImage imageNamed:[NSString stringWithFormat:#"someImage.png", img]] forState:UIControlStateNormal];
[oneButton setTag:img];
[oneButton setFrame:buttonFrame];
[oneButton addTarget:self action:#selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:oneButton];*
the action of this button show my popover view like that:
*- (void) pressButton:(id)sender {
popoverViewController *popoverFrame = [self.storyboard instantiateViewControllerWithIdentifier:#"myPopoverView"];
popoverWithObjects = [[UIPopoverController alloc] initWithContentViewController:popoverFrame];
[popoverWithObjects presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}*
from now the situation is, that i can't send to my button any message or result.
i want to say to my program button (note - i've got only sender of this button action:#selector(pressButton:) ) that popover return some result or some action of an object in popover send anything (string for example)
Or in another words when i interact with any object like button on popover view, i want to change parent buttons title label
Use NSNotificationCenter.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
You need to use delegate. Check out my answer to this similar question from this SO.
Edit: Link to tutorial on Storyboard and delegate pattern usage. And my original answer to delegate on this SO
How about writing a function on your Main View Controller to do what you want. Then call that function from the popover? (i.e. use 'prepare for segue' to send the popover the id of the Main View Controller, and then use that id to call the function on Main View from the popover)

Resources