XCODE : Changing which view controller BACK button segues to - xcode

Is there a way to change the "parent" of a view controller so you can specify which view controller the back button will segue to without creating a custom back button?
Or is the only way to create a custom back button with a custom action?

If you have access to the navigation controller, you could manually insert a view controller in to its stack of view controllers [self.navigationController setViewControllers:] before the current view controller. That would allow the navigation controller to perform its regular actions yet still navigate back to your desired view controller.

Try To create a custom button like
UIBarButtonItem * buttonBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Back" ofType:#"png"]] style:UIBarButtonItemStylePlain target:self action:#selector(onBackClick:)];
[self.navigationItem setLeftBarButtonItems:#[buttonBack] animated:YES];
and make a method..
- (IBAction)onBackClick:(id)sender
{
// your code
// Write your code what you want to do...
}

Related

Could not set the title of tableview

I embedded tableviewcontroller into a navigation controller, set the title, but it does not show when I run the app.
What is wrong?
This is the setting of the tableviewcontroller
Here is the setting of the navigation controller
First check, your NavVC must be initial VC.
If not then try this piece of code
-(void) viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
you must be presenting the plain table view instead of navigation controller. What you need to do is make you tableview as root view for a navigation controller and present the navigation controller.
var nav: UINavigationController = UINavigationController(rootViewController: vc)
//then present the nav
Change the initial VC from Navigation Controller to your TableViewController
I did not set the StoryboardId for Navigation controller.

Xcode: how to create a segue from a navigation item to a new view controller?

Maybe a silly question. But I've added a navigation item in code in my view controller:
//Set add transactions button
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonAction:)];
[self.navigationItem setRightBarButtonItem:addButton];
-(void)addButtonAction
{
}
And I want to segue from this navigation item directly to a new view controller. However I cannot create any segue from the previous view controller to the next one. And since my navigation item is created in code I cannot "pull" a segue from there to a new VC. How can I do that?
Do this
-(void)addButtonAction
{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
//If you want to present modal
[self presentModalViewController:myNewVC animated:YES];
//If you want to push
[self.navigationController pushViewController:myNewVC animated:YES];
}
Add button to dismiss if presented using modal. In case of push it will have back button

Modal view controller inside master tableview (and dismiss keyboard)

I know this is a very popular issue, but i´m having a hard time...so if anyone could help, it would most appreciate:
Im my master tableview (that i have made in storyboard) i have a plus button and every time the user presses that button, a picker view appears...depending on the selection in the picker view, a modal view appears (for this modal view, i have dragged a simple view controller in storyboard and gave it a storyboard id). Each modal view has textfields, and i want to dismiss the keyboard every time the user enters something...to create the modal view i have done this:
CobTableview *produtoCob =[self.storyboard instantiateViewControllerWithIdentifier:#"cobId"];
produtoCob.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:produtoCob animated:YES completion:nil];
produtoCob.view.superview.frame = CGRectMake(0, 0, 350, 512); //do this after presentModalViewController
produtoCob.view.superview.center = self.view.center;
produtoCob.managedObjectContext = self.contextProd;
How can i resolve this?
To everyone that has the same issue...i found out that you need both methods in your modal view controller class :
- (BOOL)disablesAutomaticKeyboardDismissal
return NO
-(BOOL)textFieldShouldReturn:(UITextField *)textField
([textField resignFirstResponder];
return yes

customized backbutton working like real back button xcode

i am making a customized back button which works exactly as a real back button does. below is my code
UIButton *cusBack = [UIButton buttonWithType:101];
[cusBack setTitle:#"Back" forState:UIControlStateNormal];
[cusBack addTarget:self action:#selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
self.view addSubView:cusBack;
and here is my selector:
-(void)clickBack{
PrevPage *pPage = [[PrevPage alloc] init];
[self.navigationController pushViewController:pPage animated:YES];
[pPage release];
}
actually, it works but the direction it creates is from RIGHT to LEFT. but I want the page to move from LEFT to RIGHT since it goes back to the recent page
thank you!
If you want to have the same behavior as the back button you should try another approach:
-(void)clickBack{ //Your presenting view controller will always be the last view controller on the stack.
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES];
}
Also, if you need to do some changes to the previous view controller, all you need to do is import it's header file and then you can use it's properties and public methods before popping the view controller.
#import "MyPreviousViewController.h"
...
-(void)clickBack{
[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1].title = #"Change title for previous view controller";
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES];
}
Currently, your back button doesn't work as the real back button does, because, instead of popping the last view controller off the stack(also releasing the objects), you actually keep pushing new view controllers on the stack. You might have some issues if this stack gets too big.

How to use custom Navigationcontroller in AppDelegate by using a Storyboard

I've got a problem concerning Navigationcontroller in AppDelegate. I'm using a storyboard, which looks like this:
As a result of using Push notifications, i've got the following function in my AppDelegate File:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}
When the notification arrives I want to initialize the "Detail View" - Controller which needs an ID as a parameter. This ID is part of my payload so it is present in didReceiveRemoteNotification.
I'd like to to the follwing:
DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[self.navigationController pushViewController:detail animated:YES];
My question at this point is: how can I get the navigation controller? I've searched for a function like "getNavigationControllerByIdentifier" or something like this, but found nothing. I can't instantiate the Detail View Controller directly because the navigationbar is missing there.
I hope you understand what I mean - if you think my approach is completly wrong please correct me ;o)
Just another small information: It's not important for me that the back button in the Detail View Controller goes back to the Table View - it's enough when it links to the controller with the button "Load Table View".
Thank you for help!
UINavigationController is a UIViewController subclass and can also be assigned an identifier in the storyboard.
Use -instantiateViewControllerWithIdentifier: to create the UINavigationController and it's root view controller. You may need to instantiate all of the intermediate controllers in code and modify the navigation controller's viewControllers property to set up the appropriate navigation stack. This way when the app launches into the detail view, they can find their way back as if they had manually pushed all the way through via the interface.
You can use rootViewController on your window object.
UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.
// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;
// You can now use it to push your next view controller:
DetailViewController *detail = [navigationController.storyboard instantiateViewControllerWithIdentifier:#"detail"];
detail.conversationID = theID;
[navigationController pushViewController:detailViewController animated:YES];

Resources