更改 UISwitch 值而不触发事件
我正在尝试根据您是否启用了位置服务来更改 UISwitch
的值。我在 ViewDidLoad
方法中设置目标事件,但因为您可以关闭应用程序并从后台加载它,所以我正在更改 UISwitch
的值在ViewDidAppear
中:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.cell2Switch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
...
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(![AppDelegate sharedAppDelegate].locationServicesEnabled && self.cell2Switch.on)
{
self.cell2Switch.userInteractionEnabled = NO;
[self.cell2Switch setOn:NO];
}
}
当我更改值时,它会触发我在ViewDidLoad
中分配的目标事件。
有没有办法可以更改 UISwitch 的值而不自动触发事件?
I'm trying to change the value of a UISwitch
based on whether you have enabled location services. I'm setting the target event in the ViewDidLoad
method, but because you could close the app the load it back up from the background, I'm changing the value of UISwitch
in the ViewDidAppear
:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.cell2Switch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
...
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(![AppDelegate sharedAppDelegate].locationServicesEnabled && self.cell2Switch.on)
{
self.cell2Switch.userInteractionEnabled = NO;
[self.cell2Switch setOn:NO];
}
}
When I change the value, it triggers the target event I assigned in the ViewDidLoad
.
Is there a way I can change the value of the UISwitch
without it automatically triggering the event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要删除目标事件。
如果
UISwitch
除了显示某些内容已启用或禁用之外没有其他用途,那么您不需要目标事件。You would need to remove the target event.
If the
UISwitch
serves no purpose other than to show you that something is enabled or disabled, then you do no need the target event.