iOS Swift create Popup Alert, Action Items Alert and Custom Alert programmatically

Swift create Popup Alert, Action Items Alert and Custom Alert programmatically
Swift create Popup Alert programmatically
Swift create Action Items Alert programmatically
Swift create Custom Alert programmatically

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Add a Simple Alert with Buttons
        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
        button.setTitle("Popup Alert", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = UIColor.lightGray
        button.addTarget(self, action: #selector(popupAlert), for: .touchUpInside)
        self.view.addSubview(button)
        
        //Add a Simple Alert with Action Items
        let button2 = UIButton(frame: CGRect(x: 100, y: 200, width: 200, height: 50))
        button2.setTitle("Action Alert", for: .normal)
        button2.setTitleColor(.black, for: .normal)
        button2.backgroundColor = UIColor.lightGray
        button2.addTarget(self, action: #selector(actionAlert), for: .touchUpInside)
        self.view.addSubview(button2)
        
        //Add a Custom Alert
        let button3 = UIButton(frame: CGRect(x: 100, y: 300, width: 200, height: 50))
        button3.setTitle("Custom Alert", for: .normal)
        button3.setTitleColor(.black, for: .normal)
        button3.backgroundColor = UIColor.lightGray
        button3.addTarget(self, action: #selector(customAlert), for: .touchUpInside)
        self.view.addSubview(button3)
        
    }

    @objc func popupAlert(sender: UIButton!){
        
        let alert = UIAlertController(title: "Did you bring your tennis racquet?",
                                    message: "Sure can't play tennis without it...",
                                    preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
            print("Great! Let's Play!")}))
        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { action in
            print("Time to head home!")}))
        
        self.present(alert, animated: true)
        
    }
    
    @objc func actionAlert(sender: UIButton!){
        
        let alert = UIAlertController(title: "Did you bring your tennis racquet?",
                                      message: "Sure can't play tennis without it...",
                                      preferredStyle: .actionSheet)
        
        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
            print("Great! Let's Play!")}))
        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { action in
            print("Time to head home!")}))
        
        self.present(alert, animated: true)
        
    }
    
    @objc func customAlert(sender: UIButton!){
        
        let alert = UIAlertController(title: "Did you bring your tennis racquet?",
                        message: "Sure can't play tennis without it. Let's get all the information...",
                        preferredStyle: .alert)
        
        alert.view.tintColor = UIColor.brown            // change text color of the buttons
        alert.view.backgroundColor = UIColor.lightGray  // change background color
        alert.view.layer.cornerRadius = 25              // change corner radius
        
        alert.addTextField { (textField: UITextField) in
            textField.keyboardAppearance = .dark
            textField.keyboardType = .default
            textField.autocorrectionType = .default
            textField.placeholder = "Brand Name"
        }
        
        alert.addTextField { (textField: UITextField) in
            textField.keyboardAppearance = .dark
            textField.keyboardType = .default
            textField.autocorrectionType = .default
            textField.placeholder = "Model"
        }
        
        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
            let brandName = alert.textFields![0]
            let model = alert.textFields![1]
            print("Great! Let's Play with \(brandName.text!) \(model.text!)!")
            
        }))
        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { action in
            print("Time to head home!")}))
        
        self.present(alert, animated: true)
        
    }

}



No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.