presentModalViewController calling any method in the target viewController? - iphone

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.

Related

How to fix the warning: view is not in the window hierarchy

I'm using the standard setup procedure for Mobclix in an iOS app and I'm calling the requestAndDisplayAdFromViewController: method from within viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[fullScreenAdViewController requestAndDisplayAdFromViewController:self];
}
here's the warning I'm getting:
Warning: Attempt to present <MobclixFullScreenAdViewController: 0x14f2c0> on <EditGameViewController: 0x1838d0> whose view is not in the window hierarchy!
I hope that's enough to go on. thanks for any help.
You're calling it from viewWillAppear - at this point, the view is not necessarily in the hierarchy yet (it WILL appear, doesn't mean it has appeared yet). Try calling it from viewDidAppear, which is called when the view is confirmed to be in the hierarchy.
Try to move your code [fullScreenAdViewController requestAndDisplayAdFromViewController:self]; to viewDidAppear.
You can also try to display the view in the viewDidLoad method.
You should try moving this code to viewDidAppear.
From the Apple documentation on viewWillAppear:
This method is called before the receiver’s view is about to be added to a view hierarchy

viewWillAppear being called twice in iOS5

I'm running all my apps to make sure it's not just one app, and in every app I have, when I run on the iOS5 simulator or device, the viewWillAppear method gets called twice on every view. I have a simple NSLog(#"1");, and this appears twice in my console every time. Is this just me, or is something going on? (It only gets called once in iOS4)
This is the code calling the view that calls viewWillAppear twice:
CloseDoorViewController *closeVC;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
closeVC = [[ CloseDoorViewController alloc] initWithNibName:#"CloseDoorViewIpad" bundle:nil];
} else {
closeVC = [[ CloseDoorViewController alloc] initWithNibName:#"CloseDoorViewController" bundle:nil];
}
[self.view addSubview:closeVC.view];
[self presentModalViewController:closeVC animated:NO];
It's the -addSubview: method.
When adding or removing view controller's view, someone must call 'View Event' methods such as -viewWillAppear:, etc. of the view controller.
Actually, it wasn't a recommended way to -addSubview:/-removeFromSuperView view controller's view by yourself before iOS 5, because it doesn't call 'View Event' methods (you can/should call it by yourself). Instead, it was recommended to use 'indirect' way to do that, such as -presentModalViewController: you use (it does call 'View Event' methods on your behalf).
On iOS 5, Apple has changed the behavior of -addSubview:/-removeFromSuperView methods to allow direct view management of view controller. So now, when you use those methods on viewController's view, 'View Event' methods will be called automatically.
So it was called twice.
See video "Implementing UIViewController Containment" on here also.
Because you are displaying the view twice.
First time by adding the view as a subview of the current view:
[self.view addSubview:closeVC.view];
Second time by pushing the view's controller on top of current view's controller:
[self presentModalViewController:closeVC animated:NO];
I'm not sure why in iOS4 the viewWillAppear was only called once, because iOS5 is correct to call it twice, given that you are displaying the view twice as explained above.
Just remove one of the lines and it would be fine (I'd recommend removing the addSubview and retain the presentModalViewController one).
If you want to restore the old (iOS 4) behavior in your view controller you should implement the following method:
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
return NO;
}
Update: since you have edited your question to include a code sample, it is clear what the problem is. Lukman's answer above is correct/excellent.
Original answer before code was included:
I would try putting a breakpoint (or log statement) in the -init method of this class. If that is hit twice, then two view controllers are being created.
(note if you have not already overridden the -init method in this class, make sure you override the designated initializer which is -[UIViewController initWithNibName:bundle:])
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html

Viewwillappear called but viewdidappear not called

the viewwillappear is called in my viewcontroller but for some reason the viewdidappear is not called.
I am showing the view by calling the navigatorcontroller push method.
I encountered this problem on ios5 system and guessed apple change the view invoke method.
Maybe you can try to write the same code in both viewwillappear and viewdidappear.

ViewWillAppear on iPad

my question is that i use ios 4.1 and viewWillAppear is not call when i go via
[self.view addSubView:self.mySecondView.view];
then and in the first view i alloc second view on ViewDidLoad .. i just want to call ViewWillApear on Second View because want to do the task dynamic.
is their some thing alternative... because InitWithNibName method also fire when alloc and init occur.. i want to fire some method when i add to that view.
Thanks
The same issue is discussed before,Just go through below SO links.
iPhone viewWillAppear not firing
viewDidLoad gets called, viewWillAppear does not get called, view does not appear on screen
http://forums.macrumors.com/showthread.php?t=575530
here is the tutorial post
http://www.idev101.com/code/User_Interface/UINavigationController/viewWillAppear.html

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.