swift ios : sử dụng UIAlert - thông báo trong swift

Hướng dẫn sử dụng UIAlert trong swift

1. Tạo project: 

Hướng đẫn : http://swiftiosswift.blogspot.com/2016/03/swift-tao-project-au-tien-bang-xcode.html

2. vào file ViewController 

Chúng ta bắt đầu viết code


2.1 Thông báo với 1 button


 @IBAction func alert(sender: UIButton) {
        
        let alertController = UIAlertController(title: "Thông báo", message: "Bạn có tin nhắn mới", preferredStyle: UIAlertControllerStyle.Alert)

        let ok = UIAlertAction(title: "ok", style: .Default, handler: { (action) -> Void in
            print("ok")
            
        })

        alertController.addAction(ok)
        
        self.presentViewController(alertController, animated: true, completion: nil)
   
    }



2.2 với 2 button

        

       @IBAction func showAlertButtonTapped(sender: UIButton) {

        let alertController = UIAlertController(title: "Thông báo", message: "Bạn có tin nhắn mới", preferredStyle: UIAlertControllerStyle.Alert)
       
        let ok = UIAlertAction(title: "ok", style: .Default, handler: { (action) -> Void in
      
            print("ok")
     
        })

        let cancel = UIAlertAction(title: "cancel", style: .Cancel, handler: { (action) -> Void in
            print("cancel")
  
        })
        
        alertController.addAction(ok)
    
        alertController.addAction(cancel)
        
        self.presentViewController(alertController, animated: true, completion: nil)
    
    }






2.3 Với 3 button 


    @IBAction func showAlertButtonTapped(sender: UIButton) {
        
        let alertController = UIAlertController(title: "Thông báo", message: "Bạn có tin nhắn mới", preferredStyle: UIAlertControllerStyle.Alert)
        
        let ok = UIAlertAction(title: "Hành động 1", style: .Default, handler: { (action) -> Void in
            
            print("Hành động 1")
            
        })
        
        let cancel = UIAlertAction(title: "Hành động 2", style: .Cancel, handler: { (action) -> Void in
            
            print("Hành động 2")
            
        })
        
        let  delete = UIAlertAction(title: "Hành động 3", style: .Destructive) { (action) -> Void in
            
            print("Hành động 3")
            
        }
        
        alertController.addAction(ok)
        
        alertController.addAction(cancel)
        
        alertController.addAction(delete)
        
        self.presentViewController(alertController, animated: true, completion: nil)
        
        

    }




Comments

Popular posts from this blog

Tranquil Tunes: Soothing Piano & Guitar Music for Study, Work, Relaxation & Meditation

Resize - Rotate - Move View or image view in swift

NSTimer - Tạo ứng dụng đếm thời gian swift-ios