首先,你們知道有任何優秀的教程網站或書籍來學習如何編寫iOS6代碼嗎?如何從另一個類調用變量
現在要解決這個問題,我有一個AppDelegate類和一個ViewController類。
我使用AppDelegate類來查找我的當前位置並將其另存爲NSString。
我在我的ViewController類中有一個標籤,我想顯示我的位置。
在java中我通常只是做這樣的事情
label.text = AppDelegate.myLocationString;
但我是相當新的目標C,我不知道該怎麼做的等價物。說實話,我甚至不確定我是否正在採取這種語言的正確方法。在目標c中是否有另一種方法來處理這個問題?
我試過四處尋找如何做到這一點,但我甚至不確定我是否正確地搜索了我的搜索詞,因此我不知道我是否在正確的方向上搜索。
可能被提供的將是非常讚賞
我的代碼,因爲它現在是任何幫助:
AppDelegate.h
#import <UIKit/UIKit.h>
#import CoreLocation/CoreLocation.h
@class BIDViewController;
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BIDViewController *viewController;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) NSString *myPosition;
@end
的LocationManager類從AppDelegate.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15) {
//if it is within the last 15 seconds, use it. Else do nothing.
if (newLocation.horizontalAccuracy < 35.0) {
//location seems pretty accurate so we will use it
NSLog(@"latitude: %+.6f, longitude: %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(@"Horizontal accuracy: %f", newLocation.horizontalAccuracy);
int latDegrees = newLocation.coordinate.latitude;
int lonDegrees = newLocation.coordinate.longitude;
double latDecimal = fabs(newLocation.coordinate.latitude - latDegrees);
double lonDecimal = fabs(newLocation.coordinate.longitude - lonDegrees);
int latMinutes = latDecimal * 60;
int lonMinutes = lonDecimal * 60;
double latSeconds = ((latDecimal * 3600) - (latMinutes * 60));
double lonSeconds = ((lonDecimal * 3600) - (lonMinutes * 60));
_myPosition = [NSString stringWithFormat:@"Lat: %d, %d, %1.4f, Lon: %d, %d, %1.4f", latDegrees, latMinutes, latSeconds, lonDegrees, lonMinutes, lonSeconds];
}
}
}
ViewController.h
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *positionLabel;
@end
ViewController.m
#import "BIDViewController.h"
#import "BIDAppDelegate.h"
@interface BIDViewController()
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//I have tried setting the label's text here but to no avail so far, I have been
//able to set it as "blah" with the following line:
//
// positionLabel.text = @"blah";
//
//and I have tried things such as the following to populate the label with the
//myPosition variable:
//
// positionLabel.text = _myPosition;
// or
// positionLabel.text = AppDelegate._myPosition;
}
- (void)viewDidUnload
{
[self setPositionLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
你總是很棒:) – 2013-02-22 14:16:16
@AnoopVaidya Shucks ... – trojanfoe 2013-02-22 14:16:38
我收到一個錯誤消息:「used type'BIDAppDelegate'wh如果需要算術或指針類型「 for this line BIDAppDelegate * appDelegate =(BIDAppDelegate)[[UIApplication sharedApplication] delegate]; – Ian 2013-02-22 14:18:01