开发者

viewDidLoad runs before instance variables are given values

开发者 https://www.devze.com 2023-04-09 02:22 出处:网络
What I have is a advertisement Class that has an instance variable that will receive the banner url:

What I have is a advertisement Class that has an instance variable that will receive the banner url:

@interface Advertisement : UIViewController {

}


@property (nonatomic, retain) NSString *image;

Now, in my app delegate I am in开发者_运维百科itializing the ad class and giving the instance variable "image" a value.

   //setup the ad
    Advertisement *theAdView = [[Advertisement alloc] init];
    theAdView.view.frame = CGRectMake(0.0, self.window.frame.size.height-120.0, 320.0, 76.0);
    theAdView.view.clipsToBounds = YES;
    theAdView.view.tag = AD_TAG;
    NSLog(@"Added the image");
    theAdView.image = theAd.banner;
    theAdView.nid = theAd.nid;
    [self.window addSubview:theAdView.view];
    [self.window bringSubviewToFront:theAdView.view];     

In the advertisement class' viewDidLoad method I want the value of the "image." However I get a null value. So it looks like the class is initializing before it gets a chance to get the instance variables.

I think I found a solution before, but I can't remember exactly what. I believe it had something to do with setting another variable that does not use (nonatomic, retain)... I'm not sure.


a UIViewController will load it's view (from nib or call the loadview method if you've overriden it) when you attempt to access it's 'view' property. In your case, 1 easy result would be to move the lines

theAdView.image = theAd.banner;
theAdView.nid = theAd.nid;

to before anything that accesses the view property, like

 theAdView.view.frame = CGRectMake(0.0, self.window.frame.size.height-120.0, 320.0, 76.0);

or a better one would be to accept the image and nid as properties in a new initializer

0

精彩评论

暂无评论...
验证码 换一张
取 消