所以我的简单想法是创建一个应用程序,允许用户通过电子邮件报告其位置的纬度和经度坐标。您点击按钮,电子邮件屏幕通过 MessageUI 框架出现,收件人、主题和正文字段已预先输入,用户所需要做的就是点击“发送”。
我的问题是,我需要将纬度和经度坐标包含在电子邮件正文中。这些坐标变量在 -(void)CLLocationManager 函数中生成并转换为字符串,就像我需要的那样。问题是,电子邮件是从另一个函数 -(void)displayComposerSheet 发送的,我不知道如何将纬度/长字符串放入要发送的电子邮件正文中。经过一番谷歌搜索后,我发现了全局变量的想法。这似乎是我需要实现的。许多消息来源都说“在应用程序委托(delegate)中声明变量,然后您就可以在代码中的任何位置使用它们”,或者至少这就是我的意思。我再次强调,我对这个游戏还很陌生。因此,似乎如果我要在 delegate.m 文件而不是项目 .m 文件中创建纬度和经度字符串,那么我将能够在闲暇时调用它们,然后将它们发送到我的电子邮件的正文。
我只是不完全是我所有“东西”应该去的地方。这是我到目前为止所得到的(效果很好)。我只需要用 CLLocationManager 生成的实际值替换默认的纬度值“12.3456”和经度值“78.9012”。任何帮助将不胜感激。谢谢!
//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];
}
最佳答案
// NSString *msgLat = self->latitude.text; do not do this
// NSString *msgLong = self->longitude.text; or this
NSString *msgLat = latitude.text;
NSString *msgLong = longitude.text;
不需要全局变量(假设两个方法属于同一个类)。
关于iphone - 使用全局变量在类之间传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2034908/