I just give u example of my problem:-
I have implemented this method:
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"update");}
inside my viewController, but it's called anytime, how I can call it manually?
Thanks in advance...
[self viewWillAppear:YES]; if you are in the object already.
It wouldn't be right to call that method directly as you are bound to do [super viewWillAppear:animated]; within it. This method is to do all the necessary set up just before the view will appear. You don't know what kind of setup the superclass does. So it is better to package the part of code that you want to reuse into a different method and call it from both the viewWillAppear: method and the other method that you want to call it from.
Related
Let me try and explain this. Within a project folder has .m and .h files (we all know that haha) however when you start a new ".m" with a xib like for example:
ViewTwoController *loginView = [[ViewTwoController alloc] initWithNibName:#"contentscreen" bundle:nil];
[self presentModalViewController:loginView animated:YES];
[loginView release];
I was wondering how do I create a method that runs as soon as that class (if I can call it that, I'm new to objective C) similar to a main method that Java would have. I want to run some code as soon as that .m has been called and started. Any help would be valued thanks :)
To be safe try to put this in - (void)viewDidLoad
Everything that happens here happens when the view is loaded onto the screen.
Note that there are number of places where you can put your method, in viewWillAppear or in your AppDelegate etc. But I found putting UI elements like your login popup better in viewDidLoad
Hope this helps...
You can add additional code in viewDidLoad method, which is called as soon as your view controller has been loaded.
Using the initWithNibName:bundle: method, your view controller will be automatically loaded and initialized.
I am not able remove view. I am adding view using addsubview method but when I use RemoveFromSuperView.
// Here I am adding view from login button
medicalViewObject=[[MedicalInfoViewController alloc] initWithNibName:#"MedicalInfoViewController" bundle:nil];
[self.view addSubview:medicalViewObject.view];
//here I am writing RemoveFromSuperView in another ViewController
-(void)BackToMainView
{
[self.view removeFromSuperview];
}
It is not working because you are sending removeFromSuperview to the wrong view object. This is what you need:
[medicalViewObject.view removeFromSuperview];
EDIT:
Your question suggests that you have a view (because you did not include the name of this view in your question, I will simply call it MainView) and then you add a subview called medicalViewObject to your MainView. You also have a method call "BackToMainView" which you want to perform the removeFromSuperview function. Your question suggests that some user action in your medicalViewObject (such as a button press) is supposed to call the "BackToMainView" method.
If this is all correct, then my answer above is correct. But based upon your comment, it sounds like you will also need to implement a delegate protocol in your medicalViewObject, and then have your "MainView" adopt the protocol.
In your declaration of the delegate in your medicalViewObject, you need to have a method call like this:
-(void)backButtonWasPressed;
and in the implementation of your MainView it should look something like this:
-(void)backButtonWasPressed
{
[medicalViewObject.view removeFromSuperview];
}
So now, whatever user action you are using in your medicalViewObject to go back to the main view (whether a button or some other object) it needs to call the following:
[delegate backButtonWasPressed];
Depending on your situation it may look a little different, but this a fairly common way to accomplish what you are trying to do. I hope this helps.
when you call [self.view removeFromSuperview]; you are telling whatever view controller you are in to remove its own view. You should be calling that line from within that exact same MedicalInfoViewController or telling medicalViewObject from the outside to remove its view like [medicalViewObject.view removeFromSuperview];
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.
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.
In this code I am loading a View Controller (and associated View) from a .xib:
-(id)initWithCoder:(NSCoder *)coder
{
// add custom initialisation code here
[super initWithCoder:coder];
return self;
}
This successfully works, but I do not really understand what the line [super initWithCoder:coder] is accomplishing. Is that initializing my View Controller after my View has been initialized?
Please be as explicit as possible when explaining. Thanks.
Your class is a subclass of UIViewController. The call is telling your super class (UIViewController) to do the steps it needs to accomplish so that you can do your init steps. This would be setting up any properties that the UIViewController provides or registering for notifications that the UIViewController needs to do its work.
It is suggested almost every time you override a method from the super class to call the super class's method in addition to the steps you need to take.
Edit: Also if you don't need to do anything in a method the superclass provides, you can just leave it out and the super class's method will be used instead. In this case I would not provide the initWithCoder: method unless there was some code you need to preform in addition to what you showed.