2011-10-17 4 views

答えて

170

私はiOSの 'チェックボックス'に精通していませんが、UISwitchを使用している場合は、デベロッパーAPIに見られるように、setOn: animated:タスクがそのトリックを行う必要があります。

- (void)setOn:(BOOL)on animated:(BOOL)animated 

だからあなたのプログラムの中にあるスイッチを設定するには、あなたが使用します。

のObjective-C

[switchName setOn:YES animated:YES]; 

スウィフト

switchName.setOn(true, animated: true) 
25

UISwitchesには、 "on"と呼ばれるプロパティを設定する必要があります。

iOSアプリやモバイルウェブサイトについてお話ししていますか?

+0

それは私が意味したものです。どうもありがとう! – Suchi

9

//使用このコード...... //は私もこのためsetOn:animated:を使用するiOSの

- (IBAction)btnSwitched:(id)sender { 
    UISwitch *switchObject = (UISwitch *)sender; 
    if(switchObject.isOn){ 
     [email protected]"Switch State is Disabled"; 
    }else{ 
     [email protected]"Switch State is Enabled"; 
    }     
2

にスイッチでオン/オフ状態の問題を解決するために、それが正常に動作します。これは、コードのUISwitchをトグルしてプリセットを読み込むために、私がアプリのviewDidLoadで使用するコードです。

// Check the status of the autoPlaySetting 
BOOL autoPlayOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoPlay"]; 

[self.autoplaySwitch setOn:autoPlayOn animated:NO]; 
+0

@jamesh素敵なコードの簡素化に感謝します!とても有難い! –

0

ViewController.h

- (IBAction)switchAction:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *lbl; 

ViewController.m

- (IBAction)switchAction:(id)sender { 

    UISwitch *mySwitch = (UISwitch *)sender; 

    if ([mySwitch isOn]) { 
     self.lbl.backgroundColor = [UIColor redColor]; 
    } else { 
     self.lbl.backgroundColor = [UIColor blueColor]; 
    } 
} 
関連する問題