以编程方式切换UIAlert Controller [英] Switch in UIAlert Controller programmatically

查看:76
本文介绍了以编程方式切换UIAlert Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在快速创建一个注册对话框,其中包含3个文本字段和一个Switch,并且我成功添加了三个文本字段两个Alert.以下代码显示了相同的内容.

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.

 let alertController = UIAlertController(title: "Register", message: "", preferredStyle: .Alert)
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        // ...

        exit(0)
    }
    alertController.addAction(cancelAction)

    let OKAction = UIAlertAction(title: "Sign UP", style: .Default) { (action) in
        // ...
        let name0 = alertController.textFields![0] as UITextField
        print("Text field: \(name0.text)")

        let email1 = alertController.textFields![1] as UITextField
        print("Text field: \(email1.text)")

        let company2 = alertController.textFields![2] as UITextField
        print("Text field: \(company2.text)")


    }

    alertController.addAction(OKAction)

    alertController.addTextFieldWithConfigurationHandler { (textField) in
        textField.placeholder = "Name"
        textField.keyboardType = .EmailAddress
    }

    alertController.addTextFieldWithConfigurationHandler { (textField) in
        textField.placeholder = "Email"
        textField.secureTextEntry = false
    }

    alertController.addTextFieldWithConfigurationHandler { (textField) in
        textField.placeholder = "Company"
        textField.secureTextEntry = false
    }


            self.presentViewController(alertController, animated: true) {
                // ...
            }

现在我需要以编程方式向Alert View添加一个开关.我们正在Swift2中进行此操作.有可能吗?,我是Swift的新手.

Now I need to add a switch programmatically to the Alert View.We are doing this in Swift2. Is it possible?, i am new to Swift.

推荐答案

这可能会对您有所帮助.

This may help you.

在上述代码中,在alertController.addAction(OKAction)之后添加此方法调用alertController.view.addSubview(createSwitch()).

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))")
}

输出:

这篇关于以编程方式切换UIAlert Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆