如何从另一个类调用变量

时间:2013-02-22 14:05:28

标签: ios variables gps

首先,您是否知道任何优秀的教程网站或书籍,以了解如何为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];

        }
    }
}

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

3 个答案:

答案 0 :(得分:3)

在ViewController中

BIDAppDelegate *appDelegate = (BIDAppDelegate *)[UIApplication sharedApplication].delegate;
[positionLabel setText:appDelegate.myPosition];

答案 1 :(得分:3)

要从视图控制器引用App Delegate,首先导入其定义(您已准备好完成):

#import "BIDAppDelegate.h"

然后在代码中获取委托:

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

然后从代表处获取位置:

CLLocation *location = appDelegate.locationManager.location;

然后在location对象上调用您喜欢的任何方法:

NSString *descr = location.description;

或者通过以下方式获得预先计算的位置:

NSString *position = appDelegate.myPosition;

答案 2 :(得分:1)

  

如何从另一个类调用变量?

假设您在ClassB

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

  

你们知道任何好的教程网站或书籍来学习如何   iOS6的代码?

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

http://www.raywenderlich.com/tutorials