我有一个视图计算位置和反向地理编码以获取邮政编码。然后它调用另一个视图,我想根据该邮政编码显示天气结果。
在第一个视图控制器中,一旦用户点击按钮翻页,我就会这样做:
- (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 presentModalViewController:weather animated:YES];
}
在WeatherApp,我想现在阅读zipcode,这是在这个视图控制器中声明的.h:
@interface WeatherApp : UIViewController{
IBOutlet UIButton *done;
MKPlacemark *zipcode;
}
@property (nonatomic, retain) MKPlacemark *zipcode;
如何使用此代码将此邮政编码传输到WeatherApp?谢谢!
答案 0 :(得分:0)
是的,这是将信息传递到新对象的好方法。
或者,您可以为WeatherApp创建自定义初始值设定项,如
- (id)initWithZipCode:(NSString *)zip;
然后在实现文件中,它可能是这样的:
- (id)initWithZipCode:(NSString *)zip
{
self = [super init];
[self setZipcode:zip];
return self;
}
最后,您可以像这样实例化该类:
- (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'
}
最后,如果你要像上面那样继续传递信息,我会问你为什么要使用initWithNibName:bundle:
。如果您只是将nil
传递给两者,为什么不使用[[WeatherApp alloc] init]
?