[英]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: 我在视图控制器A中有一个UISwith。我想将UISwitch的状态传递给视图控制器B。我正在使用ObjectiveC。这是我在视图控制器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. 我需要将UISwitch的值保存在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. 在您的交换机上设置一个IBAction,该IBAction指向拥有的视图控制器。 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. 然后,您可以从另一个视图控制器访问该属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.