2015-05-05 14 views
10

Tôi gặp sự cố khi hiển thị UIAlertController vì tôi đang cố gắng hiển thị nó trong một Lớp không phải là một ViewController.hiển thị UIAlertController bên ngoài ViewController

Tôi đã cố gắng thêm nó:

var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) 

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

mà không phải là làm việc ... tôi không tìm thấy bất kỳ giải pháp mà làm việc cho tôi được nêu ra.

+0

Bạn sắp xếp lớp học không phải là UIViewController bây giờ là một phần không thể thực hiện được. Xem xét thêm một đại biểu hoặc một cuộc gọi lại dựa trên khối để hiển thị cảnh báo trên bộ điều khiển xem đang sử dụng lớp này. – 3lvis

Trả lời

27

tôi đã viết extension này qua UIAlertController để mang lại show().
Nó sử dụng đệ quy để tìm bộ điều khiển nhìn từ trên xuống hiện tại:

extension UIAlertController { 

    func show() { 
     present(animated: true, completion: nil) 
    } 

    func present(#animated: Bool, completion: (() -> Void)?) { 
     if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController { 
      presentFromController(rootVC, animated: animated, completion: completion) 
     } 
    } 

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) { 
     if let navVC = controller as? UINavigationController, 
      let visibleVC = navVC.visibleViewController { 
       presentFromController(visibleVC, animated: animated, completion: completion) 
     } else 
     if let tabVC = controller as? UITabBarController, 
      let selectedVC = tabVC.selectedViewController { 
       presentFromController(selectedVC, animated: animated, completion: completion) 
     } else { 
      controller.presentViewController(self, animated: animated, completion: completion); 
     } 
    } 
} 

Bây giờ là dễ dàng như:

var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) 
alertController.show() 

EDIT:

Đối với Xcode 8.0 & Swift 3:

extension UIAlertController { 

    func show() { 
     present(animated: true, completion: nil) 
    } 

    func present(animated: Bool, completion: (() -> Void)?) { 
     if let rootVC = UIApplication.shared.keyWindow?.rootViewController { 
      presentFromController(controller: rootVC, animated: animated, completion: completion) 
     } 
    } 

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) { 
     if let navVC = controller as? UINavigationController, 
      let visibleVC = navVC.visibleViewController { 
      presentFromController(controller: visibleVC, animated: animated, completion: completion) 
     } else 
      if let tabVC = controller as? UITabBarController, 
       let selectedVC = tabVC.selectedViewController { 
       presentFromController(controller: selectedVC, animated: animated, completion: completion) 
      } else { 
       controller.present(self, animated: animated, completion: completion); 
     } 
    } 
} 
+0

Điều đó thật tuyệt! Cảm ơn rất nhiều – Michael

+0

Rất vui khi nó hoạt động cho bạn, tôi đã sử dụng nó với nhiều kịch bản VC (modal vc trên ngăn xếp nav bên trong thanh điều khiển thanh tab, v.v.), vui lòng cho tôi biết nếu có tình huống không hoạt động. .. –

+0

SharedApplication không thể được sử dụng trong Tiện ích ứng dụng. – SAHM

0

Tạo một hàm mà bạn gọi từ bộ điều khiển xem hiện tại và vượt qua bộ điều khiển xem hiện tại như một tham số:

func showAlertInVC(
    viewController: UIViewController, 
    title: String, 
message: String) 
{ 
    //Code to create an alert controller and display it in viewController 
} 
0

Nếu bạn giải pháp không làm việc nó có lẽ vì không có cửa sổ vào thời điểm đó . Tôi đã gặp vấn đề tương tự khi tôi cố hiển thị chế độ xem cảnh báo theo phương thức application:DidFinishLoadingWithOptions. Trong trường hợp này giải pháp của tôi là để kiểm tra xem xem gốc điều khiển có sẵn, và nếu nó không phải, sau đó thêm thông báo cho UIApplicationDidBecomeActiveNotification

NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, 
       object: nil, 
       queue: NSOperationQueue.mainQueue()) { 
        (_) in 
         //show your alert by using root view controller 
         //remove self from observing 
        } 
     } 
0

Điều này sẽ hoạt động.

UIApplication.sharedApplication().windows[0].rootViewController?.presentViewController(...) 
Các vấn đề liên quan