简体   繁体   中英

Pass UISwitch State from One View Controller to Another ViewController

I have a UISwith in a viewcontroller A. I want to pass the state of the UISwitch to viewcontroller B. I am using Objective C. This is the code I'm using in viewcontroller A:

if (![switchview isOn]) {
                NSLog(@"OFF");   
            } else {
                NSLog(@"ON");
                [comment setObject:@"YES" forKey:kESActivityPrivateKey];
            }

I need to save the value of the UISwitch in viewcontroller B.

Any help is appreciated.

Don't try to access another view controller's view hierarchy directly. You should treat another view controllers views (and a switch is a kind of view) as private.

Set an IBAction on your switch that points to the owning view controller. In that action save the sate of the switch to a property of your view controller.

You can then access the property from the other view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM