Why is viewWillAppear firing twice - iphone

Why in my UIViewController the function
- (void)viewWillAppear:(BOOL)animated
fired at two times.... Is it wrong ?
Can anyone help me?
Thanks in advance..

I used to have the same problem. If you are using Navigation Based project, the IB sets by default your RootViewController as your first view on the NavigationController. So if you push the same view again on the NavigationController in application:didFinishLaunchingWithOptions:, it will fire ViewWillApper for the second time.
If this is not the case, try putting a break point and discover when it fires it.

Related

Xcode : Storyboard and retaining data in each controller

As you can guess i am a new programmer and i have trouble getting a simple thing!
I am making an app with multiple view controllers. Each controller have textfields and UIsegmentedControl items. When i am moving from one view controller to the other (uding modal trantition if that matters), the contents of the previous one (textfield entries and segmented control option) reset to their original state. How can i make them keep their previous state?
Thanks in advance.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
bViewController *deneme = [segue destinationViewController];
[deneme setPassedValue:label.text];
}
This piece of code will solve your problem, I hope. It saves the label of whatever is inside of it. And you need to add some more code to other classes.
If this code helps you tell me and I can give you the whole code.
To save the application state you can use a model class, following the recommended MVC (model-view-controller) paradigm.
More information here: Retain view state upon reloading
As an alternative you could use the viewWillDisappear: event to save your view state, and then restore it on the viewWillAppear: event.
The viewWillDisappear: event is fired right before the view is going to disappear, and viewWillAppear: is fired before the view is put on the foreground, being ideal to make any changes to the UI.
These events might have already been declared for you in your view controller, but in case they're not check the prototypes here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
You can also use a navigation controller to move from one view to another.
This way, you will push your new view on top of the previous one, and when you go back, the previous view has kept its state.
see this tutorial for more information on storyboard and UINavigationController :
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

UITabBarController inside a UIViewController = viewDidAppear: not called

I start off with a login screen. Then after the user logs in I load a Viewcontroller with UITabBArController in it. The problem is viewdidAppear: does not get called for any of the individual viewControllers in the tabBarController.
I have a feeling that this is not the best programming practise so does anyone have any ideas how to improve the structure of my code or how to fix my problem ?
I'd guess your trouble here comes from incorrect use of UIViewController and UITabBarController.
UITabBarController exists as a container for multiple view controllers. It probably should not, itself, be contained. It's designed to sit at the top of a view controller hierarchy. So step one is probably to re-arrange your application so that the UITabBarController is no longer under anything else and see if that straightens you out.
After that, slev's approach of presenting the login view sounds like the right one.
I had a problem because I was subclassing also UITabBarControler where I have overridden viewDidAppear without calling [super viewDidAppear:...]
After calling this, viewDidAppear was called also inside sub-view-controller.
Why not make an app which is TabBarController-based, then immediately call a modal screen at app start (for your login)? After you're done with the login, just dismiss it to allow the TabBarController to become key window.
You could try to manually call viewdidAppear on the subviewcontrollers: when it's called on the rootviewcontroller also call subviewcontroller's ones manually.

reload UIViewController

I have a certain code that is executed on the viewDidLoad() of a UIViewController I have. The problem is that it is only loaded for the first time only. After it is loaded the first time and I switch to some other view and come back to this view, it doesn't call the viewDidLoad again. So where should I put this code at so that everytime this view is displayed it executes this code?
i guess in viewWillAppear
good luck
You should put it in - (void)viewDidAppear:(BOOL)animated . This gets called every time your view comes on screen.
Please refer to life cycle of iphone application
- (void)viewDidLoad
should only be called once.
so you should use,since this method is called each & every time the View is called
- (void)viewWillAppear:(BOOL)animated

presentModalViewController calling any method in the target viewController?

When i call presentModalViewController
[self presentModalViewController:iPSPvc animated:NO];
is there a method i can implement in the target viewController (iPSPvc) that gets called every time this happens?
I need to make sure some updating of the view is done.
viewDidLoad gets called when i create an instance of iPSPvc so I need a method where I can do sometime similar.
Many Thanks
-Code
Try viewWillAppear method
-(void)viewWillAppear:(BOOL)animated
{
//your code goes here
}
what's with viewWillAppear and viewDidAppear?
- (void)viewWillAppear:(BOOL)animated
If I've understood your question aright, the above method (or one its close cousins) in your target viewController. This gets called every time the controller's view is about to appear, not just when the view is first loaded.

UIViewController: viewWillAppear is called, viewDidAppear not

In a UIViewController subclass, I have the following methods:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// do something
myTextField.text = #"Default";
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// do something
[myTextField selectAll:self];
[myTextField becomeFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
// do something
myTextField.delegate = self;
}
The NIB has been created using Interface Builder. The corresponding view controller object is pushed by the navigation controller through pushViewController.
The inteded behavior is to show a default text entry in a text field, to select the entire text and to set the text field as first responder. [Edit: I've noticed that selecting all and making first responder makes no sense as the selection would dissapear; still, I wonder why the methods behave as described next.]
However, while methods viewDidLoad and viewWillAppear are called, the method viewDidAppear is not called. Can anybody tell me why? Most questions I found on the web and here deal with both viewWillAppear/viewDidAppear are not working; I also understood that in subviews or programmatically created views these methods are not evoked; but this does not apply in case and also I wonder why one of these "lifecycle" methods is evoked and the other not.
Any idea? Thanks!
I had this issue happen to me: viewWillAppear was being called but viewDidAppear was not!
I finally figured out that this was because I had a tabBarController where I overloaded it's own viewDidAppear and forgot the [super viewDidAppear:animated];
It threw off every VC in every tab! adding that line back in fixed it for my other VC's.
Hope this helps someone!
There is one case when viewWillAppear will be called but viewDidAppear will not.
Suppose you have two viewControllers and you push from the first to the second controller. Then, using the swipe, you want to go back to the first one, both controllers are displayed at the same time and at that moment viewWillAppear will be called from the first controller.
This way, you do not finish the swipe and stay on the second controller and viewDidAppear will not be called from the first controller.
I had the same problem.
I had copy/pasted viewDidAppear to create viewWillAppear but had forgotten to change the super.viewDidAppear() call. This then seemed to stop viewDidAppear from being called.
It sounds like somewhere in your code you have missed or messed-up a call to the superclass.
The call to viewDidAppear: should always follow viewWillAppear: unless you are doing something custom, which you say you don't. I don't know why it doesn't work but here are a few ideas:
Could it be that you are doing something strange in one of the delegate methods for UITextFieldDelegate? It's unlikely that it would affect viewDidAppear: being called but it could be a culprit.
Have you loaded a lot of stuff into memory before pushing the view? I'm not sure what would happen if you got a memory warning between viewWillAppear: and viewDidAppear:.
Have you tried to do a Clean? Sometimes that can help.
In cases like these when it should work I usually create a new class and the introduce the functionality one at a the time to see if I can get it work that way. I tried your code in a new Navigation Based project where I added a new UIViewController with an outlet to the text field. Then I pasted the code from the question and it did work as expected.
This can be because you added a child view controller to your parent VC in viewDidLoad or viewWillAppear. The child's appearance prevents the call to viewDidAppear.
This is a crazy thing to do, and I only know because this was a bug in my code. I meant to add the child VC to this VC, not the parent VC.