UIAlertView/UIAlertController iOS 7 and iOS 8 compatibility

后端 未结 14 1522
暗喜
暗喜 2020-11-28 22:31

I am using Swift to write an app and I need to show an alert. The app must be iOS 7 and iOS 8 compatible. Since UIAlertView has been replaced with UIAlert

相关标签:
14条回答
  • 2020-11-28 23:15

    Here is my drag and drop swift solution:

    //Alerts change in iOS8, this method is to cover iOS7 devices
    func CozAlert(title: String, message: String, action: String, sender: UIViewController){
    
        if respondsToSelector("UIAlertController"){
            var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: action, style: UIAlertActionStyle.Default, handler:nil))
            sender.presentViewController(alert, animated: true, completion: nil)
        }
        else {
            var alert = UIAlertView(title: title, message: message, delegate: sender, cancelButtonTitle:action)
            alert.show()
        }
    }
    

    Call like this:

    CozAlert("reportTitle", message: "reportText", action: "reportButton", sender: self)
    

    Beware this is only for the most basic alerts, you might need additional code for advanced stuff.

    0 讨论(0)
  • 2020-11-28 23:15

    download alert class from this link and use it easily for ios 6 ,7 and 8

    //Old code  
    **UIAlertView** *alert=[[**UIAlertView** alloc]initWithTitle:@"FreeWare" message:@"Welcome to Common class" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    
    //New code 
    
    **MyAlertView** *alert=[[**MyAlertView** alloc]initWithTitle:@"FreeWare" message:@"Welcome to Common class" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    
    0 讨论(0)
提交回复
热议问题