【问题标题】:Turn a switch Off or On on the basis of another switch state根据另一个开关状态关闭或打开一个开关
【发布时间】:2015-06-23 04:10:31
【问题描述】:

如何关闭另一个开关内的开关? 下面是一个例子 - 当 isMale 开关打开时,关闭 isFemale 开关。

- (IBAction)isMale:(id)sender {
       if ([sender isOn]) {

             //TURN isFemale SWITCH OFF

       }
       else {
            ...
       }
 }

- (IBAction)isFemale:(id)sender {
       if ([sender isOn]) {

            //TURN isMale SWITCH OFF

       }
       else {
            ...
       }
 }

我一直在阅读 Apple 开发文档,但找不到任何关于在另一个开关中更改开关的信息。 干杯。

【问题讨论】:

    标签: ios xcode uiswitch


    【解决方案1】:

    首先你需要在你的标题.h文件中创建IBOutletUISwitch

    @property (strong, nonatomic) IBOutlet UISwitch *isMale;
    @property (strong, nonatomic) IBOutlet UISwitch *isFemale;
    

    然后在您的IBAction 中执行以下操作。

    - (IBAction)isMale:(id)sender {
       if ([sender isOn]) {
    
             [_isFemale setOn:NO animated:YES];
    
       }
       else {
            // do what you want.
       }
    }
    - (IBAction)isFemale:(id)sender {
       if ([sender isOn]) {
    
            [_isMale setOn:NO animated:YES];
    
       }
       else {
            //do what you want
       }
    }
    

    请记住将您的两个 IBOutlet 连接到您的 UIViewController 中的相应对象。

    【讨论】:

      【解决方案2】:

      这是您需要的简单示例

      - (void)viewDidLoad 
       {
          [super viewDidLoad];
         // Do any additional setup after loading the view, typically from a nib.
      
          [_btnMale setOn:YES animated:YES];
          [_btnFemale setOn:NO animated:YES];
      
       }
      
      
       - (IBAction)btnMaleClick:(id)sender
       {
           if ([sender isOn])
           {
              [_btnFemale setOn:NO animated:YES];
           }
      
       }
      
       - (IBAction)btnFemaleClick:(id)sender
       {
          if ([sender isOn])
          {
             [_btnMale setOn:NO animated:YES];
          }
      
        }
      

      【讨论】:

        【解决方案3】:

        在 swift 5.1 (Xcode 11) 中使用

               [switchPass .setOn(false, animated: true)]
        

        【讨论】:

          【解决方案4】:

          这里是如何快速切换按钮状态以供将来参考

          override func viewDidLoad() {
              super.viewDidLoad()
          
              btnMale.setOn(true, animated: true)
              btnFemale.setOn(false, animated: true)
          
          }
          
          @IBAction func btnMaleClick(sender: UISwitch){
              if sender.on{
                  btnFemale.setOn(false, animated: true)
              }
          }
          @IBAction func btnFemaleClick(sender: UISwitch){
              if sender.on{
                  btnMale.setOn(false, animated: true)
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2023-01-31
            • 1970-01-01
            • 2013-10-12
            • 1970-01-01
            • 2021-10-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多