Iphone 使用全局变量在类之间传递值
因此,我的简单想法是创建一个应用程序,允许用户通过电子邮件报告其位置的纬度和经度坐标。点击按钮,电子邮件屏幕通过MessageUI框架显示,收件人、主题和正文字段已经预先输入,用户只需点击“发送”即可 我的问题是,我需要在邮件正文中包含纬度和经度坐标。这些坐标变量在-(void)CLLocationManager函数中生成,并转换为字符串,就像我需要的那样。问题是,电子邮件是从另一个函数发送的,-(void)DisplayComposer Sheet,我不知道如何将lat/long字符串放入要发送的电子邮件正文中。在谷歌搜索了一段时间后,我突然想到了全局变量的概念。这似乎是我需要实现的。很多资料都说“在应用程序委托中声明变量,然后您就可以在代码中的任何地方使用它们”,或者至少我是这么认为的。我要再次强调,我对这个游戏还相当陌生。因此,如果我在delegate.m文件而不是project.m文件中创建纬度和经度字符串,那么我就可以在空闲时调用它们,并在电子邮件正文中发送它们 我只是不知道我所有的“东西”应该放在哪里。这是我到目前为止得到的(效果非常好)。我只需要用CLLocationManager生成的实际值替换默认纬度值“12.3456”和经度值“78.9012”。任何帮助都将不胜感激。谢谢Iphone 使用全局变量在类之间传递值,iphone,objective-c,cocoa,cocoa-touch,email,Iphone,Objective C,Cocoa,Cocoa Touch,Email,因此,我的简单想法是创建一个应用程序,允许用户通过电子邮件报告其位置的纬度和经度坐标。点击按钮,电子邮件屏幕通过MessageUI框架显示,收件人、主题和正文字段已经预先输入,用户只需点击“发送”即可 我的问题是,我需要在邮件正文中包含纬度和经度坐标。这些坐标变量在-(void)CLLocationManager函数中生成,并转换为字符串,就像我需要的那样。问题是,电子邮件是从另一个函数发送的,-(void)DisplayComposer Sheet,我不知道如何将lat/long字符串放入要发
//Code that generates the Latitude and Longitude strings
//--------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//Breaks down the location into degrees, minutes, and seconds.
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latitude.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longitude.text = longt;
}
//Code that prepares the email for sending
//------------------------------------------
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"New Location Report!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"pghapps2009@gmail.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *message = @"user reported their location at:";
NSString *msgLat = @"12.3456";
NSString *msgLong = @"78.9012";
NSString *emailBody = [NSString stringWithFormat:@"%@\nLatitude = %@\nLongitude = %@", message, msgLat, msgLong];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
不需要全局变量(假设两个方法属于同一类)。不需要全局变量。只需在控制器类中创建实例变量(IVAR),并在其中隐藏lat/long。然后,在DisplayComposer Sheet方法中,使用之前计算的值。如果是同一类,则不需要在此处取消对self的引用。回答正确。为了清晰起见,编辑了。你真的不想像那样通过self来解除引用。“在谷歌搜索了一段时间后,我突然想到了全局变量的概念……很多消息来源都说“在app委托中声明变量,然后你就可以在代码中的任何地方使用它们了”不,这不是全局变量,它只是访问对象的属性(应用程序,获取其委托),然后访问该对象的属性。一个全局变量就是:一个在全局范围内的变量(不受函数或方法范围的限制,也不受实例变量的限制)。
// NSString *msgLat = self->latitude.text; do not do this
// NSString *msgLong = self->longitude.text; or this
NSString *msgLat = latitude.text;
NSString *msgLong = longitude.text;