iphone - 使用全局变量在类之间传递值

标签 iphone objective-c cocoa cocoa-touch email

所以我的简单想法是创建一个应用程序,允许用户通过电子邮件报告其位置的纬度和经度坐标。您点击按钮,电子邮件屏幕通过 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/

相关文章:

iphone - 在 iPhone 上监听 Co​​coaASyncSocket 接收连接,但新套接字不调用委托(delegate)

iphone - iOS - UIActivityIndi​​cator 导致 "Hides when stopped but is neither hidden or animating"警告

iphone - AVCaptureDevice : Flash or torch?

ios - 如何获取 TableView 的第一个单元格?

ios - 获取 iPhone 联系人电话号码类型

objective-c - 使用 Cocoa,我如何检查给定文件是否属于当前用户?

macos - 无法在 NSView 中正确定位 NSImageView

iphone - Core Data 支持带有索引的 UITableView

cocoa - 在Mac应用程序中单击一次即可获取tableView的行号

objective-c - 在子项目中配置条件代码