ios custom back button (NOT in the navi bar) - ios

i'm building my own navigation between views and i have the navi bar hidden by purpose. still, i need a button inside the view (read outside the navi bar) that would behave like "back" button. is there such method to call that could be assigned to a custom button?
i tried standard push segues, but looping back will create new instances and I just need a standard back behavior (that will destroy the current instance).
i guess it's obvious, but somehow i've been missing it. thanks.

Create a button as normal in your view, then connect it to a method that pops the view from the stack. Something like this:
- (IBAction) pressedBackButton:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}

Related

add back button to first screen of navigation controller in iOS

I know that a push segue automatically adds a back button so that the presentee can return to the presenter on click. What I want is for the back button to be available on the first UIViewController of the NavigationController (basically the only view controller in the chain that does not have a back button). How do I force add the back button?
I cannot simply add a custom image because I want the exact same chevron that comes with the iOS back button.
The easiest (yet hackish) way to achieve this is probably adding a dummy view controller in the stack before the actual first one, so you will get a proper back button. Use dummy's backBarButtonItem to customize the button title if you need, and -viewWillAppear: to respond to the button being tapped.
What I want is for the back button to be available on the first UIViewController .... How do I force add the back button
This whole thinking and logic is wrong. This is not how iOS UINavigationControllers work. You have a starting page (mainViewController) in a UINavigationControllers and from there you "move on" to something else. But you always come back to the main view controller. Just think about it, if you put a back button on the main view controller, where will the user go back to?
If you have a situation like this.
SomeViewController --> UINavigationController --> MainViewController
Once user is on SomeViewController and you take him to MainViewController which is part of UINavigationController then back button makes sense.
Just a FYI - you can easily drag and drop a UIBarButtonItem to any UINavigationController in Xcode and call it "Back". You will have to then connect that back button to another view controller.
Maybe it doesn't make sense but why can't you add a custom image? If all you want is "Back" chevron, can't you take a screenshot of this button and then put this image on your custom UIBarButtonItem?

Bypass UIView without showing

I am using Storyboards for an iOS 7 App. The root controller is a menu. For almost every view, I have a Menu button which brings the App back to that menu using [self.navigationController popToRootController:TRUE]. However, there are two UIView elements where I would like to clear the Navigation Controller view list (as happens when you pop back to the root controller), but then immediately go to another UIView without having the user see the root controller's view. Once at this new view, if the user presses the back button, I want them to go to the menu view.
I've tried to put a performSegue in the viewWillAppear, but it really messes up the Navigation Controller and views. I've tried putting a performSegue in the viewDidAppear, but the user sees first the Menu view flash in, then out on it's way to the correct view.
I hope I've explained this well enough. I hope this can be done.
Thanks.
Your best bet is to build the navigation controller stack yourself, and then use - (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated: to replace the current stack.
So you could do something like
...
UIViewController *vcToPush = ...;
NSArray *newVCStack = #[[self.navigationController.viewControllers firstObject], vcToPush];
[self.navigationController setViewControllers:newVCStack animated:YES];
This will add your new controller to the stack using the standard push animation (or not if you so choose), and after the animation is complete, set the view stack to that of the array.

Add Back Button to Toolbar in iOS

Is there some way to add a button (not in a navigation view or something like that) that will perform back (meaning, going back to the previous view before the last segue) in an iOS app?
It doesn't have the shape of a back button (it will be fine with simply the word "Back" on it).
I want to put something like that in a bottom toolbar.
Thanks.
Just put a simple button and in its action do:
[self.navigationController popViewControllerAnimated:YES];

What exactly does the back button in Navigation controller do?

Is there anyway to tell what specifically the back button from a navigation controller does? I know in terms of what it does, but in terms of method called/functions etc.
The reason is that I am hiding the navigation bar and will have a button on view instead and just want to replicate this completely.
If you want to replicate the back button feature in navigation bar, you need to call the following in that button's action:
[self.navigationController popViewControllerAnimated:YES]
In your view controller call this, when your custom back button is tapped:
[self.navigationController popViewControllerAnimated:YES];
This will pop the current view controller.
This is the UINavigationController method that the back button calls:
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
So to replicate it just do something like this in the viewController you are popping...
[self.navigationController popViewControllerAnimated:YES];
You can verify that this is exactly the method called by the back button by subclassing UINavigationController and overriding this method - you will see that it gets called when the back button is pressed (no need to do this in your app - it's just a way for you see what is going on!)
If the you're referring to the leftBarButtonItem and not the built in backButton, then it can do whatever you want. It does not automatically dismiss the current view controller. You need to hook up a selector to the barButtonItem for it to do anything. When that action is fired, you tell it what to do whether it be pop, segue, or virtually anything.

iOS - Back button on selecting a tab

I have a UITabBarController. When I select a tab other than the first, I want the tab bar hidden (which I achieved by hidesBottomBarWhenPushed) and the navigation bar to have a back button (not a plain, but default arrowed) (to get to the first tab). How do I achieve this?
I'll answer your question below, but I have to say that this is a very non-standard user interface which is mixing metaphors. If you really want to push to different view controllers, you really should not be using a tab bar. You probably should just have simple buttons to go to your secondary view controllers. I wouldn't be surprised if Apple would reject an app that used a tab bar like the way you're conceiving of it.
Anyway, to answer your question, you don't want to try to add a "back" button to the secondary views, but rather you just want to properly implement a navigation controller. Then, when you push to the secondary views, you get the back button automatically. And you also don't have to worry about silliness associated with hiding the tab bar, because when you push to another view, it will be pushed off, too.
So, consider this example, where you have a tab bar with buttons "One", "Two" and "Three". But, rather than being a proper tab bar controller configuration, you'd have something like this, where "One" is on navigation controller and has a simple tab bar user interface control on it, and if you happen to tap on the second or third tab, it will just push to the respective view. Thus, logically, it might look something like this (even if you're not using storyboards, this might help visualize the potential configuration):
So, on the main view controller ("One"), you'd add a tab bar with an IBOutlet:
#property (weak, nonatomic) IBOutlet UITabBar *tabBar;
I'd then configure that tab bar in viewDidLoad, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBar.delegate = self;
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}
Clearly, the view controller itself would need to be a UITabBarDelegate, and you'd need a didSelectItem that would perform either a push segue or use your navigationController to pushViewController. And, clearly, you will want to change the references to #"Two" and #"Three" with the actual labels you put on your tab bar buttons, but hopefully this gives you the idea:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if ([item.title isEqualToString:#"Two"])
{
// put code for pushing to second view controller here
}
if ([item.title isEqualToString:#"Three"])
{
// put code for pushing to third view controller here
}
}
I'd also want the main view controller to make sure to set the selected item back to the first item when it reappears after you pop back:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}
Anyway, if you do it this way, there's no hiding of the tab bar (because it's no longer a controller, but rather just a user interface element on the first tab). This is probably the easiest way to implement your desired user interface.

Resources