【问题标题】:Create a simple Alert in swift快速创建一个简单的警报
【发布时间】:2014-07-28 16:40:56
【问题描述】:

看来 alertstatus 不能在 swift 中使用。

你们能帮我实现下面代码的替代解决方案吗?

  [self alertStatus:@"Message!" :@"Alert title" :0];

【问题讨论】:

    标签: objective-c swift


    【解决方案1】:

    XCODE 10、IOS 12 和 Swift 4 及更高版本:

    1.简单的警报视图,对警报中的按钮没有任何操作。

    let alert = UIAlertController(title: "Error", message: "Something Went Wrong", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Ok", style: .default))
    self.present(alert, animated: true)
    

    2。对确定和取消按钮进行操作的警报:

    let refreshAlert = UIAlertController(title: "Punch Out", message: "You Will Will Punch Out", preferredStyle: UIAlertController.Style.alert)
            
    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
                print("Handle Ok logic here")
       }))
            
    refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
                print("Handle Cancel Logic here")
                refreshAlert .dismiss(animated: true, completion: nil)
       }))
    
        self.present(refreshAlert, animated: true, completion: nil)
    

    更多定制请参考StackOverflow Answer

    【讨论】:

      【解决方案2】:

      Swift 3 iOS 10

       let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
                  alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                  self.present(alert, animated: true, completion: nil)
      

      【讨论】:

        【解决方案3】:

        您可以在 3 行中执行此操作。确保添加“确定”按钮或“操作”,这将有助于消除警报,否则您最终会在警报上冻结屏幕

            let myMessage = "This is an alert"
        
            let myAlert = UIAlertController(title: myMessage, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
        
            myAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        
            self.presentViewController(myAlert, animated: true, completion: nil)
        

        【讨论】:

          【解决方案4】:

          试试下面的代码

          let alert = UIAlertView()
          alert.title = "Title"
          alert.message = "My message"
          alert.addButtonWithTitle("Ok")
          alert.show()
          

          但在 iOS 8

          UIAlertView 已弃用。所以使用UIAlertControllerpreferredStyleUIAlertControllerStyleAlert。应该是

          var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
          alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
          self.presentViewController(alert, animated: true, completion: nil)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-23
            • 2011-06-07
            • 2019-08-20
            • 1970-01-01
            • 2015-06-18
            相关资源
            最近更新 更多