2

まず、iOS6のコーディング方法を学ぶための優れたチュートリアルサイトや本を知っていますか?

目前の問題については、AppDelegateクラスとViewControllerクラスがあります。

AppDelegateクラスを使用して現在の場所を検索し、NSStringとして保存しています。

ViewControllerクラスに、現在地を表示するラベルがあります。

Javaでは、私は通常、このようなことをします

label.text = AppDelegate.myLocationString;

しかし、私はObjective cにかなり慣れていないので、同等の方法がわかりません。正直なところ、この言語に対して正しいアプローチを取っているかどうかさえわかりません。Objective 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
4

3 に答える 3

3

ViewControllerで

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

ビューコントローラからAppDelegateを参照するには、最初にその定義をインポートします(これは準備ができています)。

#import "BIDAppDelegate.h"

次に、コードでデリゲートを取得します。

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

次に、デリゲートから場所を取得します。

CLLocation *location = appDelegate.locationManager.location;

次に、ロケーションオブジェクトで任意のメソッドを呼び出します。

NSString *descr = location.description;

または、次のコマンドで事前に計算された位置を取得します。

NSString *position = appDelegate.myPosition;
于 2013-02-22T14:11:51.637 に答える
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

于 2013-02-22T14:11:15.150 に答える