I need to detect the Navigation BackBarButton and View while switching tab on Tab based application .
How can i achieve this. i need code sample
NavigationBar is drawn after the viewController that is pushed loads. Also, that modalViewController's sit ontop of the topViewController in the 'stack' on the navigationController.
and to detect a UINavigationController's back button press is by verifying that the current view controller is not present in the in the navigation controller's view controller stack.
It can safely check this condition in - (void)viewDidDisappear:(BOOL)animated as logically, by the time that method is called it would be extremely likely that the view controller was removed from the stack.
Switching view or the same view can be detect by using navigationController.topViewController and backBarButtonItem is triggered by using isKindOfClass.
Here is the example that works for me.
- (void)viewDidDisappear:(BOOL)animated{
if ([self.navigationController.topViewController isKindOfClass:[SDWebImageRootViewController class]]) {
NSLog(#"Is kind of");
//condition goes here
}
if (!self.navigationController.topViewController) {
NSLog(#"Is kind of topViewController");
//condition goes here
}
}
Related
I don't want to let the navigation controller show its navigation bar in the whole project.
Now there are three view controllers
(1) Login view controller
(2) Sign up view controller
(3) Home view controller.
I just hope to use action(which can be triggered by any kind of event, i.e. drag gesture, not necessary the pressing button) to switch between these view controllers. But I found once I get to the "signup view controllers", I can not go back to the login view controller, since there is no "BACK" navigation bar.
Questions:
How "PUSH" in one view controller, then "POP" in the other view controller?
Or there is some different way to solve this problem?
Thank you so much, any suggestion is great.
To programmaticaly go backward in a navigation controller's navigation stack, call this method:
[self popViewControllerAnimated:YES];
When and where you call this is up to how you want your app to flow. Essentially, the default navigation controller calls this automatically when the navbar's back button is pressed. But if you hide the navbar and still need to pop back, you can call this method to pop back.
As for pushing forward, it's simply a matter of creating a Push segue on the storyboard, giving it a name, and then in your code, call this method:
[self performSegueWithIdentifier:#"segue_YOUR_SEGUE_ID" sender:self];
On the question of your app, what probably makes most sense is for the login view be a view by itself. It should contain a modal segue to a sign up view for new users as well as a modal segue to the home view controller (which may or may not need to be embedded in a navigation controller).
Performing a modal segue works exactly the same as a push segue (if you're using storyboards. Hook up the segue, choose a modal segue, then call the performSegueWithIdentifier: method in your code when you need the segue to occur.
Dismissing a modal view is slightly different, but still quite simple. It goes like this:
[self dismissViewControllerAnimated:YES completion:nil];
It's fairly check to do with an 'if' statement...
if (self.navigationController.navigationBarHidden == NO) {
//YOUR ACTION
}
Hope that helps!
I'm currently working on an iOS app in Xcode.
I have a few View Controllers that are part of a tab bar controller. I also have another view controller, that I access with a push from a button on one of the tabs (modal segue). I also have a back button on this extra view which is also a segue that leads back to the view controller where we came from.
However, when I press this back button, and come back on the view controller that was part of the tab controller, the tab bar at the bottom is no longer displayed.
Why is this happening and how can I solve this?
Try this, this will help you
- (void)viewWillDisappear:(BOOL)animated
{
self.hidesBottomBarWhenPushed = NO;
}
Try adding ViewController.hidesBottomBarWhenPushed = NO; when you are popping back to the viewcontroller.
I have built an app based on a tab bar controller. While I am on one of the tab views I want to be able to swipe my finger and switch to another tab view of which the index on the tabBarController in unknown. I am therefore calling the desired viewcontroller from its nib. The view gets swapped correctly but the problem is that it appears ABOVE the tab bar itself, covering it completely and making it become unusable! How can I push the view back or the tab bar up? Thanks
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender
{
DesiredViewController *controllerInstance =[[DesiredViewController alloc]initWithNibName:#"DesiredViewController" bundle:nil];
controllerInstance.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controllerInstance animated:YES];
}
presentModalViewController:animated always uses whole screen: On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
If you really use UITabBarController, it instantiates view controllers of its tabs. So trying to instantiate view controller of one of tabs manually is wrong. You will end in a mess of view controllers instances.
Set property selectedIndex of your UITabBarController to switch tab. I think you need this. But it will be switched without animation.
If you know only pointer of controller you want to switch to, ask viewControllers property about controller's index and than switch tab by its index:
self.selectedIndex = [[self viewControllers] indexOfObject:viewController];
I'm building an app that combines tabbar and navigation bar. In one of tabs, I have a UIButton and by touching it, I push a Tableview into stack of navigationcontroller. Then, without returning to the root view controller manually by pressing Back buttons I change to another tab from tabbar and when I come back to the tab with the tableview inside, I get bad access error.
I've already tried popping the tableview from navigation controllers stack, or not releasing the tableview but I could't make it.
Thanks...
Assuming you've created your tabBar and Navigation Controllers in the AppDelegate, you can code your app to return each tab to the root view when it is selected.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController == firstViewNavigationController) {
[firstViewNavigationController popToRootViewControllerAnimated:NO];
} else if (viewController == secondViewNavigationController) {
[secondViewNavigationController popToRootViewControllerAnimated:NO];
}
}
I am using this and it is working fine in my app. I am releasing my Navigation Controllers in the dealloc method. I have also made my AppDelegate conform to the UITabBarDelegate protocol.
I've got a view that sometimes appears as a pushed view of uinavigationcontroller, and sometime just as the initial view of a tab-bar item.
There's a 'Save' button on the interface which I want to make the view pop back to previous view when it's been pushed onto screen, and do nothing when it's being displayed as part of a tab bar selected screen.
In pseudo-code, I guess what I want to do is:
if view-has-been-pushed, then pop back, else do nothing
How can I tell if the view has been pushed?
As per documentation
NSArray* views = [myNavigationController viewControllers];
if (self == [views objectAtIndex:0])
{
// I am the root view
}
but as jasarien said, popViewControllerAnimated does nothing anyway if the view is already the root
Your "if view-has-been-pushed, then pop back, else do nothing" logic is easily implemented with something like:
if (self.navigationController != nil) {
// We are part of a navigation controller, so pop
}
You probably want to remove the Done button if you are not in a navigation controller? You can do the same check in viewDidLoad and show or hide the Done button there.
You could get the view controllers property from the navigation controller and compare against the first controller in the array. If the comparison returns true, then it's the root view controller, else it's been pushed.
However, if a view controller is the root view controller, calling pop should just not do anything, so you shouldn't need any extra logic.