简体   繁体   English

以编程方式打开/关闭 UISwitch iOS

[英]Turn On/Off UISwitch Programmatically iOS

I have a UISwitch embedded in UITableviewCell.我在 UITableviewCell 中嵌入了一个 UISwitch。 The tableview uses dynamic prototype cells. tableview 使用动态原型单元格。 The following code controls the switch action when selected.以下代码控制选择时的开关动作。 It works good and turns the switch on and off and runs the code.它运行良好,打开和关闭开关并运行代码。 However, when I programmatically call the button action, it runs the code but doesn't not change the selected position from off to on or vice versa.但是,当我以编程方式调用按钮操作时,它会运行代码但不会将所选位置从关闭更改为开启,反之亦然。 Why would it not show the switch ON/OFF change?为什么它不显示开关 ON/OFF 变化?

// Call button programmatically

    [self didHideFlags:self];

    - (IBAction)didHideFlags:(id)sender{
    _isOnFlag = !_isOnFlag;
    if (_isOnFlag){
        [self.flagOverlay.graphics removeAllObjects];
        [self.flagTableView reloadData];
    }else{
        NSLog(@"Option 2");
        [self updateMyFlagsWitAlert:NO];
    }
}

在此处输入图片说明 在此处输入图片说明

Create IBOutlet for your UISwitch and when you call the action programmatically use it as sender, Or you can do it yourself when the action is triggered just change change UISwitch selected state manually,为您的 UISwitch 创建 IBOutlet,当您以编程方式调用操作时,将其用作发送者,或者您可以在触发操作时自行完成,只需手动更改 UISwitch 选定状态,

Hope this will help.希望这会有所帮助。

The action method responds to a change in the switch's state, but the calling the action method yourself won't change the switch state.动作方法响应开关状态的变化,但自己调用动作方法不会改变开关状态。

You need to set the switch's isOn property to turn it on or off.您需要设置开关的isOn属性来打开或关闭它。

UISwitch subclasses UIControl . UISwitch UIControl子类。 Using Interface Builder, you've effectively added an action method named didHideFlags(_:) for the UIEvent.valueChanged event with a target equal to whichever instance implements the didHideFlags(_:) function:使用 Interface Builder,您已经有效地为UIEvent.valueChanged事件添加了一个名为didHideFlags(_:)的操作方法,其目标等于实现didHideFlags(_:)函数的任何实例:

switch.addTarget(<myTarget>, action: #selector(didHideFlags(_:)), for controlEvents: .valueChanged)

From the UIControl documentation ( The Target-Action Mechanism ):来自UIControl文档( 目标操作机制):

Controls use the target-action mechanism to report interesting events happening to your code.控件使用目标-动作机制来报告发生在您的代码中的有趣事件。 The target-action mechanism simplifies the code that you write to use controls in your app.目标操作机制简化了您为在应用程序中使用控件而编写的代码。 Instead of writing code to track touch events, you write action methods to respond to control-specific events.您无需编写代码来跟踪触摸事件,而是编写操作方法来响应特定于控件的事件。 For example, you might write an action method that responds to changes in the value of a slider.例如,您可以编写一个动作方法来响应滑块值的变化。 The control handles all the work of tracking incoming touch events and determining when to call your methods.该控件处理跟踪传入触摸事件和确定何时调用您的方法的所有工作。

Action methods are not responsible for causing a UIControl 's standard behavior, such as a UISwitch toggling on or off.操作方法不负责导致UIControl的标准行为,例如UISwitch打开或关闭。 Rather, action methods implement side effects that occur in response to events.相反,动作方法实现了响应事件而发生的副作用。

As Paulw11 suggests , to programmatically toggle the UISwitch on or off, mutate its isOn property or call its setOn(_:animated:) method: 正如 Paulw11 建议的那样,以编程方式打开或关闭UISwitch ,改变其isOn属性或调用其setOn(_:animated:)方法:

switch.isOn = false

or或者

switch.setOn(false, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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