2

首先,你们知道任何好的教程网站或书籍来学习如何为 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

AppDelegate.m 中的 LocationManager 类

- (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];

        }
    }
}

视图控制器.h

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *positionLabel;

@end

视图控制器.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
4

3 回答 3

3

In your ViewController

BIDAppDelegate *appDelegate = (BIDAppDelegate *)[UIApplication sharedApplication].delegate;
[positionLabel setText:appDelegate.myPosition];
于 2013-02-22T14:11:13.997 回答
3

To reference the App Delegate from your view controller, first import its definition (which you've ready done):

#import "BIDAppDelegate.h"

and then in code get the delegate:

BIDAppDelegate *appDelegate = (BIDAppDelegate *)[[UIApplication sharedApplication] delegate];

and then get the location from the delegate:

CLLocation *location = appDelegate.locationManager.location;

and then call whatever method you like on the location object:

NSString *descr = location.description;

Or get the pre-calculated position with:

NSString *position = appDelegate.myPosition;
于 2013-02-22T14:11:51.637 回答
1

How to call a variable from another class?

Assume you are in ClassB

ClassA *aObj=... ; //create an object of ClassA
aObj.propertyName=...;//set the property or ivar
NSString *string=aObj.propertyName;//retrieve it

do you guys know of any good tutorial sites or books to learn how to code for iOS6?

http://www.lynda.com/Apple-training-tutorials/106-0.html

http://www.raywenderlich.com/tutorials

于 2013-02-22T14:11:15.150 回答