私はこれについて他のほとんどの投稿を見てきましたが、私はそれを得ることができません。私はiOSとobjective-Cの新人です。Objective-CとiOSの両方のBig Nerd Ranchガイドに従っています。私が自分のサイトで見つけた課題の1つは、単純なCoreLocationアプリケーションを作ることです。コードをクラスに分けようとしているので、後で再利用することができます。ボタンを使って別のクラスのメソッドを呼び出す方法iOS
私のViewController.mファイル内のボタンを取得して、MyLocation.mファイル内のlocationManager(MyLocationクラス)を呼び出したいとします。私はNSNotificaitonを調べましたが、正しく動作するようにはできませんでした。
私の人生では、私が逃していることや間違っていることを理解できません。私は質問の簡潔さを謝り、できるだけ多くの基本を学びたいと思っています。以下は
が私のファイルです:
ViewController.h:
#import <UIKit/UIKit.h>
#import "MyLocation.h"
@interface ViewController : UIViewController
- (IBAction)GetLocation:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *LocationOutput;
@end
ViewController.m:
#import "ViewController.h"
#import "MyLocation.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize LocationOutput;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setLocationOutput:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)GetLocation:(id)sender {
//WHAT GOES HERE?
}
@end
MyLocation.h:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@class MyLocation;
@protocol MyLocationDelegate <NSObject>;
@end
@interface MyLocation : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
MyLocation.m:
#import "MyLocation.h"
@implementation MyLocation
@synthesize locationManager = _locationManager;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Latitude is: %f", newLocation.coordinate.latitude);
}
@end
私はまだラベルに何も出力していないと知っていますが、私はNSLogをやっています。
ありがとうございました。ほんとうにありがとう。
ありがとうございました。これははるかに明確になります。しかし、私のプログラムはbottonが '[myLocation locationManager]'の行に当たると停止します。 MyLocation.mの '@synthesize locationManager = _locationManager;'に行き、何もしません。私はそれに何が欠けていますか?私は今日まで、私はもっと快適に感じていたと思った。ありがとうございました。 – Siriss
サンプルを再確認してください。コード私は訂正しました。オブジェクトをインスタンス変数に割り当てるときは、自動リリースしないでください! – pgb
更新していただきありがとうございます。私は自動レファレンスを内蔵していますので、オートレリーズはしていませんでしたが、それは同じラインで止まってしまっています。 locationManagerメソッドが見つかりません。 – Siriss