开发者

why I can't transfer variables between two view controllers this way?

I have one view who calculates location and reverser geocode to get the zip code. Then it calls another view where I want to display weather results based on that zip code.

In the first view controller I do this once user clicks on the button to turn the page:

- (IBAction) showMyWeather : (id)sender {

WeatherApp *weather = [[WeatherApp alloc] initWithNibName:nil bundle:nil];
weather.zipcode = placemarkZip; //this one seems not to be doing the job
weather.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentMo开发者_运维问答dalViewController:weather animated:YES];

}

And at the WeatherApp I would like to read now zipcode, which is declared in this view controller .h:

@interface WeatherApp : UIViewController{

IBOutlet UIButton *done;
MKPlacemark *zipcode;
}

@property (nonatomic, retain) MKPlacemark *zipcode;

How can I use this code to transfer this zipcode to the WeatherApp? Thanks!


Yes, this is a fine way to pass information into your new object.

Alternatively, you could create a custom initializer for WeatherApp like

- (id)initWithZipCode:(NSString *)zip;

and then in the implementation file, it could be like this:

- (id)initWithZipCode:(NSString *)zip
{
    self = [super init];
    [self setZipcode:zip];
    return self;
}

Finally, you could instantiate the class like so:

- (IBAction)showMyWeather:(id)sender
{
    WeatherApp *weather = [[WeatherApp alloc] initWithZipCode:placemarkZip];
    [weather setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:weather animated:YES];
    [weather release]; // No longer needed with ARC... just sayin'
}

Finally, if you're going to continue to pass in information as you do above, I'd question why you're using initWithNibName:bundle:. If you're just going to pass nil to both, why not just use [[WeatherApp alloc] init]?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜