iphone development controls null after ViewDidLoad
I am a newbie to both Iphone dev and obj c. I am trying to call a web service when the view is loaded and display a activity indicator during the time it takes to retrieve result from the web service.
My problem is that once the ViewDidLoad
is completed, my activityindicator
and labels are getting null. Hence after the web service call I am not able to manipulate any of the controls.
I am not able to understand why this is happening; obviously it is my lack of understanding of the basics. I done a bit of homework on this but I have not reached anywhere. It would very helpful if some could please explain why the controls get null once the viewdidload
is complete.
Update: thank you for the answers. On viewdidload
, I am doing the following, I am animating an activity indicator and assigning some values to my textview
. Then I call a web service. Debugging the code, I realized that once viewdidload
is complete and -(void) connection: (NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)
is called, my text view/activityindicator gets null.
(void)viewDidLoad {
[activityindicator startAnimating];
if(myarray != NULL) {
myuitextview.text = @""; // displaying some values from array
@try{
// call web service
} @catch (NSException *ns) {
}
}
}
开发者_JS百科
Welcome to Objective-C! :) The start can be a bit rough, but once you get used to it, it's great..
It's hard to answer your question without code, cause variables can "turn null" for many reasons. However since you're a newbie, it probably has to do with properties, retaining, etc.
There are obviously many related posts on the subject. Here's one: Objective-C 101 (retain vs assign) NSString
Basically in your .h file you need:
#import <UIKit/UIKit.h>
@interface Car:NSObject{
NSString *name;
}
@property(nonatomic, retain) NSString *name;
And then in your .m file in viewDidLoad:
self.name = [[[NSString alloc] init] autorelease];
// For strings, can use convinient function:
// self.name = [NSString string];
It's very important to use self.name and not just name as the former calls the property setter method and the latter does not (important for retaining the property correctly).
HTH, It's obviously general, but so is your question :) Oded.
精彩评论