Something like prepareForSegue but for when going back - ios

In my iOS app, I'm using a UINavigationController with segues setup in Xcode. For one of the views, I want to hide the navigation bar, and for the others, I want it to show up. I am able to successfully hide the bar in the main view, then nicely animate it in when I segue to the next view, but when I go back (using the back button in the navigation bar), the bar just disappears, leaving a black rectangle, then switches back to the previous view. I would like to be able to catch this, with something like the opposite of prepareForSegue, and nicely animate the navigation bar out. Is there some way to do this?

There currently is no prepareForDesegue:sender: alternative to prepareForSegue:sender:. The recommended practice is to establish a reference, in the destination ViewController, back to the source ViewController. Then, when the destination ViewController is dismissed, it can notify the source ViewController that it is about to become the top ViewController again.
Typically, the reference is established in prepareForSegue:sender:.
So, to make this concrete, let's suppose that you have ViewControllerA, and are about to segue to ViewControllerB. In ViewControllerB, you would define a property that references ViewControllerA. (This is often done using protocols, but to make it simple, just assume that ViewControllerB has #property ViewControllerA *delegate;.)
Then, in prepareForSegue:sender:, you would do the following:
ViewControllerB * vcB = (ViewControllerB *)[segue destinationViewController];
vcB.delegate = self;
Later, in ViewControllerB, in whatever code is about to get you back to ViewControllerA, you would use self.delegate to reach back to ViewControllerA, and let it know it's about to be presented, and give it the opportunity to do whatever you need to with the UINavigationBar.

In the view's UIViewController that you want the navigation bar to appear, place the following methods:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
You can add some logic in case you want the bar to stick around for any reason (like certain next views still need the bar).

Related

UINavigationController back button return to FIrstTableViewController

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.

Why is adding a navigation controller making my array disappear?

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

UINavigation Controller subclass and push Segues

I've been looking this up for a while now, it might have a simple answer:
According to the Apple docs, past ios6, we can subclass UINavigationController. How do we perform a segue from identifier when it prevents anything that isn't a UINavigationController. Mainly:
uncaught exception 'NSGenericException', reason: 'Could not find a
navigation controllerfor segue 'profileSegue'. Push segues can only
be used when the source controller is managed by an instance of
UINavigationController.
I'm using JaSidePanels and my center panel (navigation) needed to be subclasses for a delegate as there is a menu on the left panel that I want to switch the views when clicked.
#interface CenterViewController : UINavigationController <MenuDelegate>
Basically, since this object is a CenterViewController at runtime, is there a way to cast it to its superclass? I've tried [self superclass] but that didn't work (same error).
I need to call this code in the CenterViewController. Is it possible to move it to UINavigationController?
- (void)viewDidLoad
{
RootViewController *rootViewController = (RootViewController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
MenuViewController *leftViewController = (MenuViewController *)rootViewController.leftPanel;
// Store a reference to the center view controller in the left view controller's delegate property
leftViewController.menuDelegate = self;
[super viewDidLoad];
}
- (void) MenuItemSelected: (NSString*) item
{
if ([item isEqualToString:#"Home"]) {
//These would throw the error since we're not technically a "UINavigationController"
//[self performSegueWithIdentifier: #"mapViewController" sender: nil];
} else if ([item isEqualToString:#"Profile"]) {
//[self performSegueWithIdentifier: #"profileSegue" sender: self];
}
}
EDIT:
More information with pictures.
I'm curious as to how navigation controllers should work with side panels. I've looked at many other plugins for sidepanels and here is an example. Even though it works, why does it have 2 nav controllers?
Here is our current setup:
So basically, am I thinking about this wrong in the sense that I want to push a new VC to the existing NavVC? Would it be better to push a new NavVC when a menu button is pressed? What would happen when we go into a subview from the Maps view. Would the menu be accessible via sliding?
If you look carefully at the message you will see that your problem isn't caused by subclassing UINavigationController it is because you are executing the segue against your (subclassed) UINavigationController. Push segues are executed against a UIViewController that is embedded in or managed by a UINavigationController, with the system then finding the managing UINavigationController via the view controller's navigationController property in order to execute the push.
The message you have received says
...Push segues can only
be used when the source controller is managed by an instance of
UINavigationController
In your case the source controller is an instance of UINavigationController, it isn't managed by a UINavigationController.
You haven't said exactly how your app navigation works, but I have a suspicion that a UINavigationController isn't the right tool to use anyway. If you are using the side menus to allow the user to select the central content in a random way (i.e. the user could select the first option then the fifth and then go back to the first) then you should probably just have a central view into which you present the selected view. Pushing views onto a UINavigationController will end up with a large 'stack' of views unless you pop the current view controller before pushing the new one, which is even more complicated and not the visual effect you are looking for.
You can still achieve the 'push' style transition but you will need to use a custom segue. If it were me I would probably push from the left if the user selected a menu item that was closer to the top than the current option and from the right if the new item was closer to the bottom than the current, but again I am making assumptions on how your app navigation works.
UPDATE
Yes, I think you are on the right track with the section of your updated question. Navigation controllers are for navigating a series of related views in a hierarchical manner - think of the Settings app - you select "general" or "wall paper" or whatever - each of these then has a series of views that you can navigate through; up and down a stack.
In your app it looks like home, profile and settings should each be navigation controllers. Your root view would then just be a view. Your menu would select which view controller to present in the root view - this is like a tab bar controller, except your menu takes the place of a tab bar.
You can allocate your home, profile & settings view controllers in your appDelegate and store them to properties of the appDelegate. Then you can use something like this:
- (void) MenuItemSelected: (NSString*) item
{
myappDelegate *app=(myappDelegate *)[UIApplication sharedApplication].delegate;
[delegate.currentViewController removeFromParentViewController];
UIViewController *newController;
if ([item isEqualToString:#"Home"]) {
newController=app.homeViewController;
} else if ([item isEqualToString:#"Profile"]) {
newController=app.profileViewController;
}
if (app.currentViewController != newController)
{
[app.currentViewController removeFromParentViewController];
[app.rootViewController addChildViewController:newController];
app.currentViewController = newController;
}

I have view controllers in a navigation controller hierarchy, but only want the navigation bar on the first view controller - how do I do this?

I have a main view controller that lists a bunch of items, and when they tap on one of the items it segues them to the next view. However, in the next view, I don't want the navigation bar to be there, I only want it in the first view (I'm using a UIToolBar for the navigation bar, kind of like in iBooks).
How exactly do I go about achieving this? If I remove the main view controller from the navigation controller completely (unembedding, effectively) I can implement the nav bars selectively, but this solution doesn't allow segues, so it's no good.
My other solution was to call self.navigationController.navigationBarHidden = YES; in viewDidAppear for the second view, but with this, the UIToolBar that I added in my storyboard is pushed under the navigation bar that hasn't been hidden yet, and then when it does get hidden, it disappears and the UIToolBar "falls down", which is a pretty gross effect to the user.
What would be the best way to go about getting this effect?
The best way to do that is the following. In you viewWillAppear: in the first (rootViewController) of your UINavigationController, you set:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
and then in you viewWillDisappear, you the the opposite:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
This way should work without nothing 'ugly' happening to the user.

Interface Builder IB - Navigation Controller, skipping a view

Hello I am new to iOS and have a question about navigating through my views.
I am using IB wiring up the PREVIOUS and NEXT buttons in the nav bar that pushes my views. This all works fine. However I am having trouble finding out where exactly what I need to do or where I need to place the code so I can skip over a view. To simplify my situation...
-I have 1 Nav Controller and 4 View Controllers named VC1, VC2, VC3, VC4. Each VC has a .H/.M
-Starting from VC1, they follow one after the other.
Say I want to skip VC3 and jump right to VC4 based on a setting in VC2. Where would I put the code to do this? Would I need to unhook the IBAction method from the NAV buttons at VC3?
I do apologize if this has been covered before. If there is a tut or if you know of a post that answers this, please let me know. I did do a search but the search was returning generic posts probably due to me using the wrong terminology.
Thanks in advance.
A couple of thoughts:
If you want to push from VC2 to either VC3 or to VC4, rather than having VC3 immediately push to VC4 in special cases, I think it's better to just have VC2 push directly to the appropriate view controller. Thus you might have an IBAction in VC2 that would do this for you:
- (IBAction)pushToNext:(id)sender
{
BOOL skipToVC4 = ... // put in whatever logic you'd use to bypass VC3 and go directly to VC4
UIViewController *nextController;
if (skipToVC4)
{
nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC4"];
// obviously, if using NIBs, you'd do something like:
// nextController = [[ViewController4 alloc] initWithNibName:#"VC4" bundle:nil];
}
else
{
nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC3"];
}
[self.navigationController pushViewController:nextController animated:YES];
}
This way, when you pop back from VC4, you'll pop back directly to the appropriate view controller (e.g. if you pushed from VC2 to VC4, when you pop, you'll pop right back to VC2 automatically.
And, obviously, if you're using storyboards, rather than manually invoking pushViewController, you could have two segues from VC2 (one to VC3 and one to VC4), give them appropriate identifiers, and then just invoke performSegueWithIdentifier to segue to the appropriate view controller. But the idea is the same: You can define an IBAction that performs the appropriate segue depending upon whatever logic you so choose.
You say that you have "PREVIOUS and NEXT buttons in the nav bar that pushes my views", I wonder about your "PREVIOUS" button. Is that doing a popViewControllerAnimated? Generally, a "NEXT" button will push to a new view controller, but the "PREVIOUS" button should not push to the previous view, but pop back to it. If you don't pop back, you can end up with multiple instances of some of your prior view controllers. Thus, the "PREVIOUS" button should be linked to an IBOutlet that does something like:
- (IBAction)popToPrevious:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
When popping back, you'll obviously pop back to the view controller that you pushed from. If you want to skip a few of the view controllers as you're popping back in iOS versions prior to 6.0, you would use popToViewController or popToRootViewControllerAnimated. For example, let's say that you pushed from VC1 to VC2, to VC3, to VC4. If you want to pop back from VC4 all the way to VC1, you would hook up and IBAction in VC4 like:
- (IBAction)popToRoot:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Or, if you wanted to pop back from VC4 to VC2, you would
- (IBAction)popToVC2:(id)sender
{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[ViewController2 class]])
{
[self.navigationController popToViewController:controller animated:YES];
return;
}
}
}
You can avoid this iteration through the navigationController.viewControllers if you passed a reference of VC2 to VC3 and then again to VC4, but sometimes the above technique is easier.
By the way, if you're supporting iOS 6 and above, only, and are using storyboards, you can also use unwind segues, which are a more elegant way of popping back to a particular view controller. But it's not clear whether (a) you're using storyboards; and (b) you're supporting iOS 6 and above only, so I'll refrain from a discussion of unwind segues at this point.
First:
Show us some code, where you are pushing new view controllers, maybe your whole navigation controller code
Second (Solution):
I assume:
Your prev/next buttons are linked to your navigationController class
you have the appropriate methods (prevPressed:/nextPressed:), which are called, when you click one of the buttons
I can help you with the following:
you know which controller is visible at the moment with the visibleViewController #property
each time you click on a button in the navBar you can ask the visibleViewController which next/previous view controller should be pushed/popped
Best solution would be, if all of your controllers VC1/2/3/4 are a subclass of a viewController class, which defines a method in it's interface:
- (Class)nextViewControllerClass;
- (Class)previousViewControllerClass;
and in the implementation:
- (Class)nextViewControllerClass {
return [VC4 class];
}
- (Class)previousViewControllerClass {
return [VC1 class];
}
And in your navigationController code the do this:
- (IBAction)next:(id)sender {
UIViewController *nextViewController = [[[self.visibleViewController nextViewControllerClass] alloc] init];
[self pushViewController:nextViewController animated:YES];
}

Resources