Switch in UIAlert Controller programmatically

后端 未结 4 1648
天命终不由人
天命终不由人 2021-02-06 09:50

I am creating an registration dialog in swift with 3 text field and one Switch and I successfully add three text field two the Alert. The following code shows the same.

4条回答
  •  無奈伤痛
    2021-02-06 10:45

    This may help you.

    Add this method call alertController.view.addSubview(createSwitch()) in above code after alertController.addAction(OKAction).

    func createSwitch () -> UISwitch{
    
    
        let switchControl = UISwitch(frame:CGRectMake(10, 20, 0, 0));
        switchControl.on = true
        switchControl.setOn(true, animated: false);
        switchControl.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged);
        return switchControl
    }
    
    func switchValueDidChange(sender:UISwitch!){
    
        print("Switch Value : \(sender.on))")
    }
    

    OutPut :

提交回复
热议问题