In my tab-bar I have four viewcontrollers, and what happens in one can affect the view of the other, so I may need to reload some elements in the viewcontroller when it becomes visible. Normally I'd fix that by implementing viewWillAppear, but when I switch between the tabs, viewWillAppear does not seem to get called. How can I fix that, or what should I do instead?
Update: as a PS I should add that this is a tabbarcontroller in a navigationcontroller hierarchy
Cheers
Nik
You may use the tabbar controller delegate works like a charm
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[viewController viewWillAppear:YES];
}
Please see my answer here
iPhone viewWillAppear not firing
And in case you find this question because you would like to update something in the UITabBarController itself, not the UIViewControllers of a UITabBarController, like the OP's question. For example, hiding or displaying a custom UITabBarButton. In Swift 3.0 overriding setNeedsStatusBarAppearanceUpdate of my UITabBarController class worked for me.
override func setNeedsStatusBarAppearanceUpdate() {
}
viewWillAppear should only be used when the view appears, and not for updating it.
Use setNeedsDisplay on the viewController's view instead.
Related
I have an application where a TableView Controller segues to a ViewController and then to another.
I want to maximize real estate in the TVC and use
[self.navigationController setNavigationBarHidden: YES animated:YES];
to hide the bar. However, the bar returns to the view on the return from segue.
The app was constructed in storyboard for 5.1 using Xcode 4.3.1.
Can anyone assist by telling me where I can place a similar instruction to remove the Navbar on return from segue OR suggest an alternative method.
I have exhausted all means in storyboard by changing attributes for the controllers involved. However what you see in Storyboard isn't always what I get.
I found the answer above in the comment so just documenting it because I used it and it worked well! Thanks to #Tomasz.
To prevent the navigation bar reappearing in the header once you go back with the segue, use the following:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden: YES animated:YES];
}
Swift solution
override func viewWillAppear(animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
Thanks #spacemonkey and #Tomasz, this worked.
How to remove the transition effect from a modal segue when displaying the modal like this:
[self performSegueWithIdentifier:#"SomeIdentifier" sender:self];
I know I can go into the storyboard and toggle between 4 different animations but I don't want any! How do I remove them?
I know I could say presentModalViewController animated: NO but I do not and can not call it this way. I need to use the performSegueWithIdentifier method.
In the storyboard you can select your segue and in the Attributes Inspector uncheck "Animates". That should do it.
Here's the full source of a no-animation segue:
BVNoAnimationSegue.h
#import <UIKit/UIKit.h>
#interface BVNoAnimationSegue : UIStoryboardSegue
#end
BVNoAnimationSegue.m
#import "BVNoAnimationSegue.h"
#implementation BVNoAnimationSegue
- (void)perform
{
[[self sourceViewController] presentModalViewController:[self destinationViewController] animated:NO];
}
#end
To use this, add the files to your project (e.g. as BVNoAnimationSegue.m/.h) then in storyboard, select 'Custom' as your Segue type and type BVNoAnimationSegue in the Segue Class box. After you've done this Xcode seems to be clever enough to add 'no animation segue' as an option when you CTRL-drag between UIViewControllers in future.
You need to make a custom segue (without the animation) if you need a segue but don't want the animation.
You should look at Apples "creating custom segues" example in the view controller programming guide, they do a custom modal segue without an animation (just like you wanted).
One more way we can
YourViewController *aYourViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"aYourViewControllerIdentifier"];
[self.navigationController pushViewController:aYourViewController animated:NO];
and add the #"aYourViewControllerIdentifier" to view controller in your storyboard.
I've got a Tab Controller with a navigation controller which has a view, as seen in the image below:
I need to retrieve the Switches Controller from within my AppDelegate so I can do some things with it at runtime.
I believe I can retrieve the NavigationController itself by doing this:
UINavigationController *navController = [tabBarController.viewControllers objectAtIndex:0];
Not really sure how to access my SwitchesController from there though. Any suggestions?
Why not make an ivar with an IBOutlet? It's probably the most flexible solution as you can now change the ordering of your viewController's without breaking your build.
Use one of the visibleViewController, topViewController properties of UINaviationController, or call:
[navigationController.viewControllers objectAtIndex:index];
Docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Hope it helps.
My application is view based in my app in one view i write view will appear but this method doesn't call. My code is
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"view will Appear");
[tableView reloadData];
}
Can anyone tell why viewWillAppear method not called.
Sorry I forgot To tell You That This method call first time but when i remove a subview from this view viewWillAppear not called. Please Suggest me how to solve this problem.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(#"view will Appear");
[tableView reloadData];
}
if still it is not calling then try to call through code , as per example [classobj viewWillAppear:NO];
The viewWillAppear and other related methods are called to the viewcontroller that are linked with the rootViewController of the mainWindow.
So if you are using a view based application, the viewWillAppear method of the first view controller will work properly and others wont.
I think that the problem you are seeing is that viewWillAppear: is a method on a UIViewController, and not a method on UIView. viewWillAppear: is called on the view controller to indicate that the controller's view will become visible.
If you have added the code above to a class based on UIView, that code won't get called. You need to move that code to your view controller -or- you might achieve the result that you are looking for by implementing the didMoveToSuperview method in your UIView based class instead.
didMoveToSuperview will be called on your view when your view is added to another view using addSubview:.
Hi Here I did instead
[self.view addSubview:viewcontroller.view];
I Used this:
[self presentModalViewController:viewcontroller animated:YES];
and write this method now my problem is solved.
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"==========view will appear");
}
I have a navigation controller which present one modal viewController. From inside this modal viewController I present another modal viewController. All I want is to get back from the last modal viewController to the navigationController (the root viewController).Something similar with popToRootViewController, but adapted for modalViewControllers;
NavigationController -> present Modal ViewController A -> present Modal ViewController B
From modal ViewCOntroller B I want to return to navigationCOntroller.
Is this possible?
Appreciate,
Alex.
In iOS 5 you need to do
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]
Edit: As of iOS 6 dismissModalViewControllerAnimated: is deprecated.
You need to call
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{ // Do something on completion}]
Problem solved :)
I tried
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
and works.
Thanks.