2016-11-08 8 views
0

ViewControllerからNSObjectクラスファイルにアクションを接続して渡したいと思っています。私のviewcontrollerには、UISwitch Buttonがあり、私が切り替えると、NSObjectクラスファイルでそれを動作させたいのです。viewcontrollerをNSObjectクラスファイルに接続する方法は?

ビューコントローラ(UIView)からスイッチがONになっていると、アクションはNSObjectクラスファイルで動作する必要があります。

Objective-Cでコーディングしています。

ここに私ですViewController.mこれはViewControllerファイルでのみ行いました。 NSOBjectクラスファイルに接続されていません。

- (void)viewDidLoad{ 

[super viewDidLoad]; 

//UISwitch work at viewDidload 

[self.mySwitch addTarget:self 
        action:@selector(stateChanged:) forControlEvents:UIControlEventValueChanged]; } 


- (void)stateChanged:(UISwitch *)switchState{ 

NSLog(@"current calendar: %@", [[NSCalendar currentCalendar] calendarIdentifier]); 
//get the date today 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateStyle:NSDateFormatterMediumStyle]; 
[formatter setCalendar:[NSCalendar currentCalendar]]; 
NSString *dateToday = [formatter stringFromDate:[NSDate date]]; 
NSLog(@"Today Date is: %@", [NSDate date]); 
self.myLabel.text=dateToday; 

if ([switchState isOn]) { 

    //[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"[email protected]=japanese"]]; 
    [formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese]]; 

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 

    NSString *dateToday = [formatter stringFromDate:[NSDate date]]; 

    UILabel *dtDate = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)]; 
    [dtDate setText:dateToday]; 

    self.myLabel.text =dateToday; 
    NSString *locale = [[NSLocale currentLocale] localeIdentifier]; 
    NSLog(@"current date is: %@", dateToday); 
    NSLog(@"current locale: %@", locale); 

} else { 

    //[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; 
    [formatter setCalendar:[NSCalendar autoupdatingCurrentCalendar]]; 
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    NSString *dateToday = [formatter stringFromDate:[NSDate date]]; 

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)]; 
    [myLabel setText:dateToday]; 

    self.myLabel.text =dateToday; 
    //  NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    //  [formatter setDateStyle:NSDateFormatterMediumStyle]; 
    NSString *locale = [[NSLocale currentLocale] localeIdentifier]; 
    NSLog(@"current locale: %@", locale); 
    NSLog(@"%@", [formatter stringFromDate: [NSDate date]]); 
    } 
} 

答えて

0

あなただけのアクションが必要な場合、あなたはスイッチからのコールバックを取得する際、他のクラスメソッドはNSObjectの拡張クラスでグローバル変数または通知オブザーバを作成し使用しています。

- (void)callback:(id)sender 
{ 
    [MyObject switchState:sender.state]; 
} 
+0

あなたが言ったように使用しました。エラーが見つかりました。私はNSObjectクラスファイルでこれを使用します。 - (void)コールバック:(id)送信者 { [NSObject switchState:sender.state]; } エラーは次のように表示されます: '__strong id'タイプのオブジェクトにProperty 'state'ノートが見つかりました ありがとうございます。あなたは詳細を教えてくれますか?私はiOSの始まりです。 –

+0

NSObjectから拡張された新しいクラスを作成し、その中にスイッチ状態クラスのメソッドを追加します。 – santhosh

+0

NSObjectクラスのアクションをトリガーするだけであれば、self.mySwitchのaddTargetをNSObjectサブクラスのオブジェクトに設定し、そのNSObjectサブクラスにstateChanged:メソッドを定義します。 – Sreekanth

関連する問題