I am launching a viewController from another view controller via the push on its table views cell. Now on the second view controller I have a whole bunch of controls mainly test fields. I would like to by using the default back button provided in the second view controller so it'll be the title of the first view controller and keep in mind like I said default, so I don't want to create my own button for back on the second view controller. So would like to detect if the second view controller is exiting or disappearing or will disappear and based on certain conditions stop it from going back to the original caller view controller. I originally assumed it could be done in here:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound)
{
// So back button was pressed on the second view controller, so how do I stop it
// here from going back to the original view controller.
}
}
Or how do I do it? I can't seem to find a view controller return type BOOL method to call and stop it.
Thanks.
Note that method is called viewWillDisappear, not viewShouldDisappear, so Apple won't let you stop the view from disappearing at that point. And from the user's point of view, pressing the back button should indeed take them back. Instead you might consider hiding the back button under the circumstances where it's not allowed.
You can't prevent the view controller from closing however you can emulate it.
This line will replace the close button with a button that will call a homemade function
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(closeBtn)];
So you will be able to close the view if you want:
- (void) closeBtn
{
bool shouldClose=true;
// Your conditions here
if (shouldClose) [self dismissViewControllerAnimated:YES completion:nil];
}
sorry,i'm not good at english,so reading so long text is a little difficult for me.i can only get that you want to go back without a button. i known navigationController has a method to go back to previous view.
Make a custom back button and use self.navigationItem.hidesBackButton=YES
Add target to your custom back button and use it as you like. When you want to move the view you can use [self.navigationController popViewcontroller].
Related
I have a navigation view that contains a tableview. If one of the tableview items is clicked, the navigation controller passes the user to the detail view.
This usually works perfectly. However, if the user clicks the back button from the detail view and moves back and forth between the tableview and detail view too quickly, the app will sometimes become glitchy.
This glitch occurs rarely, but when it does, the back button makes the view transition to the left, but it just shows the detail view again. Then I need to press the back button again in order to actually go back to the list view.
Does anyone know why this might be happening? Or is there a bit of code I can post to help resolve this? Thanks!
Try this:
//in viewwillappear
appDelegate.window.userInteractionEnabled = FALSE;
[self performSelector:#selector(userInteraction) withObject:nil afterDelay:0.5];
-(void)userInteraction
{
appDelegate.window.userInteractionEnabled = TRUE;
}
I want to change between two view controllers using a button, on an iPhone. I don't want to use UITabBarController because it takes too much space, but I want the same kind of segue. How do I do that? Do I need to write a custom segue ?
A Push Segue is probably what you want as its operation can be reversed allowing the button (or stepper) to dictate navigation.
To initiate;
[self performSegueWithIdentifier:#"DestinationViewController" sender:self];
To return either use the back button that will appear in the navigation bar or programmatically with a hidden back button.
[self.navigationController popViewControllerAnimated:YES];
To hide the back button:
self.navigationItem.hidesBackButton = YES;
called in the -(void)viewWillAppear part of the lifecycle.
I'm having an annoying problem which takes the best off me :<
I've got 3 view controllers, one to show an advertisement in detail which also has a toolbar. Now what I want is, if the user presses the facebook icon on my toolbar in the first view it has to perform a check. If the check turns out false it needs to go to a 2nd view which shows a login screen. If the user logs in here it has to go to a 3rd screen which shows a simple input + button to update their status.
When the user is at this third screen there should be a button "Back", but this button shouldn't bring them back to View2 but it should bring them back to View1 (the advertisement detail screen).
I figured that I wanted to show the 2nd screen (if check turns false) without pushing it but keeping the NavigationBar + TabBar presented. I added some screenshots to clarify.
First view
Second view
I want this view to be presented without using PushViewController but keep the NavigationBar and TabBar.
Third View
I hope this is enough information, hopefully someone can help me.
Thanks in advance!
Perhaps the most natural thing to do here is to present the login view controller modally. When the user has logged in successfully, the first controller can then push the third view controller onto the navigation stack. This way, the back button will lead directly back to the first view controller, and the user will understand why.
So if we have three UIVIewControllers:
DetailViewController
FacebookLoginViewController
UpdateViewController
We have two viable options:
1) Upon successful login...pop the current LoginViewController and then push the UpdateViewController
PopViewController(detailViewController, false);
PushViewController(updateViewController, true);
2) Present the login Modally and simply present the UpdateViewController
PushModalViewController(loginViewController, true);
//ascertain result of login
if(IsLoggedIn) {
PushViewController(updateViewController, true);
}
consider the following view push logic.
if (!login) {
LoginViewController *lvc = [[[LoginViewController alloc] init] autorelease];
[self.navigationController pushViewController:lvc];
}
else {
ThirdViewController *tvc = [[[ThirdViewController alloc] init] autorelease];
[self.navigationController pushViewController:tvc];
}
It is : create and push the view controller when it is needed.
Try to pop the current view controller (without animation i guess) before pushing the new one. If there is only one view controller in the navigation stack no back button will be shown.
Haven't used Monotouch but i guess something like this:
this.NavigationController.PopViewControllerAnimated(false);
this.NavigationController.PushViewController(newViewController, true);
But as Caleb suggests it's probably better figure out a way that fits the normal navigation behaviours instead of hacking around it.
I have a a UIViewController that is pushed to by two different views in my app.
One time it is a modal view, so I have the right navbar button set to Done and it dismisses the view.
At another time in my app, this same view is pushed to, but not modally, thus I don't want this button to show. I tried adding this when pushing it, but no luck.
self.navigationItem.rightBarButtonItem.enabled = NO;
You can check the parent view controller for whether it has the modalViewController property set
if (self.parentViewController.modalViewController == self)
{
// add button
}
Simple and effective -
self.navigationItem.rightBarButtonItem = nil;
Edit:
How can you add this when you are pushing this ? Add it in the viewWillAppear or viewDidLoad of the viewController you want to see this is in.
You can check for a certain condition.
If it is pushed from view 1, you can make it nil.
If it is shown modally from view 2, you can make it appear.
For this, you will have to make the viewControllers communicate with each other. For that, you will need to use NSUserDefaults and set an integer for a key.
You can assign two different integers logically and use them as the condition for showing/not showing the rightBarButtonItem.
Good Luck.
Is there a way to connect code to a view's 'back' button that is part of a navigation controller? That back button is automatically coded in by the nature of it being a navigation controller so I am not sure how I can connect code to it.
(I know this should be really easy but I can't seem to find it in the documentation on navigation controller.)
viewWillDisappear is where you would typically add code to execute when the back button is pressed
In reality you should not have a code that needs to be executed when Back button is pressed. What exactly you're trying to achieve?
You could check if the controller was popped in viewWillDisapper, like in this example:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(#"Article done");
}
}