【问题标题】:Show an AlertView from a UIView?从 UIView 显示 AlertView?
【发布时间】:2014-08-30 11:02:28
【问题描述】:

在 Swift 中,UIAlertView 已弃用并替换为 UIAlertController。正如我所见,显示UIAlertController 的唯一方法是通过UIViewController,但有时您想通过UIView 来显示它。

现在 IOS8 和 Swift 可以做到吗?

【问题讨论】:

标签: swift ios8 uialertcontroller


【解决方案1】:

UIAlertView 在 Swift 中仍然可用,它仅在 iOS 7 中被弃用。如果您想使用最新的 iOS 8 API,我建议您执行以下操作。虽然为简单起见,如果您的目标是 iOS 7 和 iOS 8,我建议您仅使用标准 UIAlertView 而不要实现 UIAlertController。

import UIKit

class ViewController: UIViewController, UIAlertViewDelegate {

let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue

// MARK: - IBActions

@IBAction func showAlertTapped(sender: AnyObject) {
    showAlert()
}

// MARK: - Internal

func showAlert() {

    if iosVersion >= 8 {
        var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)

        // The order in which we add the buttons matters.
        // Add the Cancel button first to match the iOS 7 default style,
        // where the cancel button is at index 0.
        alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
            self.handelCancel()
        }))

        alert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
            self.handelConfirm()
        }))

        presentViewController(alert, animated: true, completion: nil)
    } else {
        var alert = UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Confrim")

        alert.show()
    }

}

func handelConfirm() {
    println("Confirm tapped")

    // Your code
}

func handelCancel() {
    println("Cancel tapped")

    // Your code
}

// MARK: - UIAlertViewDelegate

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 0 {
        handelCancel()
    } else {
        handelConfirm()
    }
}

}

【讨论】:

    【解决方案2】:
            class MyViewController: UIViewController {
    @IBOutlet var myUIView: MyUIView
    }
    
            class MyUIView: UIView {
    func showAlert() {
    
            let parentViewController: UIViewController = UIApplication.sharedApplication().windows[1].rootViewController
    
            var alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
    
                println("clicked OK")
            }))
            alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in
    
                println("clicked Cancel")
    
            }))
            parentViewController.presentViewController(alert, animated: true, completion: nil)
    
    }
            - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
            {           
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 2012-10-10
      • 2014-10-19
      • 2023-04-06
      • 1970-01-01
      • 2013-02-01
      • 2014-03-27
      相关资源
      最近更新 更多