Swift/UISwitch: how to implement a delegate/listener

后端 未结 7 1654
慢半拍i
慢半拍i 2020-12-29 19:24

In my UITableViewController I have a custom cell which contains a switcher which is the following:

import Foundation
import UIKit

class SwitchCell: UITableV         


        
相关标签:
7条回答
  • 2020-12-29 19:56

    In Swift4.0

    mySwitch.addTarget(self, action: #selector(valueChange), for:UIControlEvents.valueChanged)
    
     @objc func valueChange(mySwitch: UISwitch) {
            let value = mySwitch.isOn
            // Do something
            print("switch value changed \(value)")
        }
    
    0 讨论(0)
  • 2020-12-29 19:56

    In Swift 5

    switchDemo.addTarget(self, action: #selector(didTapAdvertise), for:UIControl.Event.valueChanged)
    
    @objc func didTapAdvertise(mySwitch: UISwitch) {
            let value = mySwitch.isOn
            // Do something
            print("switch value changed \(value)")
        }
    
    0 讨论(0)
  • 2020-12-29 20:01

    UISwitch has no delegate protocol. You can listen to the status as follows:

    ObjC:

    // somewhere in your setup:
    [self.mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    
    
    - (void)switchChanged:(UISwitch *)sender {
       // Do something
       BOOL value = sender.on;
    }
    

    Swift:

    mySwitch.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
    
    func switchChanged(mySwitch: UISwitch) {
       let value = mySwitch.on
       // Do something
    }
    

    Swift3 :

    mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControlEvents.valueChanged)
    
    func switchChanged(mySwitch: UISwitch) {
        let value = mySwitch.isOn
        // Do something
    }
    

    Swift4:

    mySwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)
    
    @objc func switchChanged(mySwitch: UISwitch) {
        let value = mySwitch.isOn
        // Do something
    }
    
    0 讨论(0)
  • 2020-12-29 20:02

    Swift 3:

    @IBOutlet weak var mySwitch: UISwitch!  
    
    mySwitch.addTarget(self, action: #selector(MyClass.switchChanged(_:)), for: UIControlEvents.valueChanged)
    
    func switchChanged(_ mySwitch: UISwitch) {
        if mySwitch.isOn {
            // handle on
        } else {
            // handle off
        }
    }
    
    0 讨论(0)
  • 2020-12-29 20:04

    I have the solution in objective-c, it is the method that I use regularly:

    -The Action of the switch must be in tableviewcontroller and not on the cell

    -When You tap on the switch inside the action can do this to find the correct cell, then you can easily find the index or any other value that you need ...

    - (IBAction)switchValueChanged:(UISwitch *)sender
    {
        YourCellClass *cell = (YourCellClass *)[sender findSuperViewWithClass:[YourCellClass class]];
            etc....
        }
    

    the method findSuperviewWithClass is a category on UIView

    - (UIView *)findSuperViewWithClass:(Class)superViewClass
    {
        UIView *superView = self.superview;
        UIView *foundSuperView = nil;
    
        while (nil != superView && nil == foundSuperView)
        {
            if ([superView isKindOfClass:superViewClass])
            {
                foundSuperView = superView;
            } else
            {
                superView = superView.superview;
            }
        }
        return foundSuperView;
    }
    
    0 讨论(0)
  • 2020-12-29 20:19

    Swift 3:

    Using Storyboard Autolayout:

    Add Reference:

    @IBOutlet weak var sampleSwitch: UISwitch!
    

    Associate method:

    @IBAction func sampleSwitchValueChanged(_ sender: Any) {
    
        if sampleSwitch.isOn {
            print("ON")  
        }
        else {
            print ("OFF")
        }
    }
    

    Programatic way:

    Adding Target:

    sampleSwitch.addTarget(self, action: #selector(ViewController.sampleSwitchValueChanged(sender:)), for: UIControlEvents.valueChanged)
    

    The method associated with the switch:

       func sampleSwitchValueChanged(sender: UISwitch!)
        {
            if sender.isOn {
    
                print("switch on")
    
            } else {
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题