So I was create three UITableViewControllers with UINavigationController. I want a back button on 3rd UITableViewController, what returns my view to first UITableViewController instead of second.
How can I do that? That must be a real backButton, not a image or something else. Will be perfect to do this only with storyboard.
UPDATE
Perhaps I poorly explained what I want.
I don't want use any button with action on it. I just want something like as setting "address" of 1st TableViewController on my default back button. There is any way to do it?
add a button and connect it to following action
- (IBAction)backToFirstView:(UIButton *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
(or)
[self.navigationController popToViewController:yourFirstViewControllerObject animated:YES];
}
There are different ways to navigate from DetailViewController to other view controllers.
We will go through the cases one by one.
First of all I would like to clear that if its your default
navigation bar's back button, then it must return to the last most
view controller only which is actually a default behavior of a
navigation controller.
Second, If you would like to go back to the
last most view controller on the tap of a button placed by you, you
should write the following code
[self.navigationController popToViewController:NAME_OF_A_VIEWCONTROLLER animated:YES];
Third, If you would like to go to the first view controller from where you
started, you should write the following code
[self.navigationController popToRootViewControllerAnimated:YES];
Ok, I found a way to resolve my problem. Thanks for your answers guys, they was very helpful.
So for resolve this problem you just need use link what give me Kumar KL upper, and wrote next method in your UITableVIewController
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self.navigationController popViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
Now you got a backButton what redirect you to your viewController, BUT title of this button is wrong. Let's resolve that unfair.
Create new class CustomSegueцрфе inherited from UIStoryboardSegue with next code in CustomSegue.m :
- (void)perform
{
UIViewController *sourceView = (UIViewController *) self.sourceViewController;
UIViewController *destinationView = (UIViewController *) self.destinationViewController;
[[destinationView navigationItem] setTitle:#"TitleOfYourViewController" ] ;
[sourceView.navigationItem setTitle:#"TitleOfButton"] ;
[sourceView.navigationController pushViewController:destinationView animated:YES];
}
Now you can go to storyboard and connect 2nd ViewController with 3rd with custom segue.
Like you see UINavigationController uses Title of previous ViewController for button title, so you just need change it.
Related
I am trying to navigate to "Home" view controller and for this I have written the following code in the ContainerViewController. But once the code executes, the application hangs and it show 100% CPU usage. Please help.
- (IBAction) home:(UIButton *)sender
{
HomeViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self.navigationController pushViewController:homeViewController animated:YES];
//[self presentViewController:homeViewController animated:YES completion:nil];
}
I have a question for you
1-If You want to push SecondViewController on to FirstViewController then your code is good enough
2-If you have a containerview in firstViewController and you want to add SecondViewcontroller's view to firstViewController
then use this code
UIViewController*vc1 = [[test1 alloc]initWithNibName:#"SecondViewController" bundle:nil];
//add to the container vc which is self
[self addChildViewController:vc1];
//the entry view (will be removed from it superview later by the api)
[self.view addSubview:vc1.view];
I think you want an unwind segue here. In your first view controller add :
- (IBAction)unwindToFirstViewController:(UIStoryboardSegue*)sender
{
}
You then need to hook up each of your view controllers home button to the green Exit button at the bottom of the view controller, choosing the unwindToMainMenu option. This will then take you back to the first view controller when pressed.
Have you tried popping the current view?
navigationController?.popViewControllerAnimated(true)
or just popping to root?
navigationController?.popToRootViewControllerAnimated(true)
or setting a new stack?
navigationController?.setViewControllers(homeViewController, animated: true)
The code is in Swift but it would work the same in ObjectiveC
I am trying to open a ViewController2 from another ViewController1. I am not having any buttons, because the second ViewController should start after a sound has finished.
My function from ViewController1, that should call ViewController2:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"audioPlayerDidFinishPlaying");
//Here it should call the other ViewController
}
How can I handle this?
Try this... it's the same thing as doing it with a button:
MyViewController* controller = [[MyViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
If you are using story board. Right click and drag from your source button to your destination view controller and then select push.
If you want to do it in code make sure you have a navigation viewcontroller then do as the answer suggested in regards to pushing the next view.
Currently I'm using Unwind to get to the Main View of its Path.
This is the code I've on the Sender ViewController:
- (IBAction)showRecents:(UIStoryboardSegue *)segue {
[self.navigationController.tabBarController setSelectedIndex:1];
NSLog(#"Root of Navigation Executed");
}
This is how it looks before unwinding
And this is how it looks after unwinding from the source view controller:
The issue is that I don't understand why it's not showing the Green TopBar? Has something to do with the TabBarController?
This is my Path VC:
The answer to this Question is to. First we need to Hide/Show the NavigationController on the Source ViewController and Before it disappear We need to Show it again. That Way on the Sender ViewController You will be able to see the Navigation Because it was set to be shown on the Source where the unwind its performed.
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO];
}
I am new to coding objective-C and am trying to use pop to viewController. To do this when a button is tapped i use
[self.navigationController popToViewController:(what goes here) animated:YES];
I know its a UIViewController* that goes there but I am wondering where do I declare this and what code would I use to declare it.
My basic storyboard is I have 4 view controller A,B,C,D and A is the root which pushed to B which pushes to C which pushes to D, my button is on D and I am trying to pop back to B. The object at index method won't work because its doesn't always go A->B->C->D sometimes it goes A->C->B->D so
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex:2] animated:YES];
So that doesn't work.
Thanks for the help in advance. Sorry if this question is too basic.
You need a way to find the desired view controller to pop to.
-(IBAction)popToDesiredViewController:(id)sender
{
UIViewController *desiredVC = nil;
// LOOK AT ALL VIEW CONTROLLERS IN NAVIGATION CONTROLLER
for (UIViewController *oneVC in self.navigationController.viewControllers) {
// CHECK IF THIS IS THE VIEW CONTROLLER YOU WANT
// change this to your logic
BOOL foundDesiredVC = [oneVC isKindOfClass: [SignInVC class]];
if (foundDesiredVC) {
desiredVC = oneVC;
break;
}
}
[self.navigationController popToViewController:desiredVC animated:YES];
}
Personally I don't find this as a basic question, even I got struck in such a situation. But I found a good solution by using “Mediator Design pattern” and develop my custom UINavigationController as a coordinator by combining the method:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
and maintaining my own navigation stack.
A coordinator (your custom UINavigationController) has to have the control over your navigation, if your are having unpredictable way of navigation.
Currently I have a UIViewController with a couple of buttons that when pressed move to a UITableViewController. Each button loads a specific array of data to the UITableViewController by identifying the segue of the specific button and displaying the corresponding data.
This works fine as is.
However I wish to add an embedded UINavigationController so I can navigate through the UITableViewController and and corresponding views while still being able to 'press back' to the initial UIViewController.
Firstly, where am I meant to put this. I tried over the tableViewController and 2 things happen -
a) If the segues still go to the UITableViewController, there is no navigation displayed.
b) If I move the segues to go to the UINavigationController, none of my arrays show in the tableViewController, but I do have navigation.
Where do I link my segues or where do I embed the UINavigationController so this works?
(I haven't put any code as I don't think this will involve it, but if it does just let me know and I will add).
Not sure if I'm missing something but, it's not working. I want to be able to still utilize the buttons I already have and not use the buttons on the navigation from the UIViewController to the UITableViewController.
For Hiding the Navigation on root
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
The ViewController which has 2 buttons can be made as rootViewController to the NavigationController. The NavigationController can be added to the window.
Window -> NavigationController -> UIViewController (as rootView)
Hope this helps.
Select the root view controller->editor->embed in->navigation controller