iOS-プログラムでUISwitchを設定する方法


104

プログラムでUISwitchをオンまたはオフに設定したい。どうすればいいですか?私はiOS初心者です。

回答:


195

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には、「オン」と呼ばれるプロパティを設定する必要があります。

iOSアプリまたはモバイルWebサイトについて話しているのですか?


10

このコードを使用して、iOSのスイッチのオン/オフ状態の問題を解決します

- (IBAction)btnSwitched:(id)sender {
    UISwitch *switchObject = (UISwitch *)sender;
    if(switchObject.isOn){
        self.lblShow.text=@"Switch State is Disabled";
    }else{
        self.lblShow.text=@"Switch State is Enabled";
    }                

この行に賛成:UISwitch *switchObject = (UISwitch *)sender;
ユーザーではないユーザー

2

私もsetOn:animated:これを使用しており、正常に動作します。これは、アプリでコードviewDidLoadを切り替えてUISwitchプリセットをロードするために使用するコードです。

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

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

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];   
    }
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.