2016-04-06 29 views
1

Bittiğinde bir uyarı veren bir zamanlayıcı var. .Şu andaki UIViewController'da şu an UIAlertController var

yolu ben bu şimdi yapıyorum vermek: Bu uyarı görünüm kullanıcı şu anda görünümü denetleyicisi sunulmalıdır

Benim duygu bu çok daha etkili aşağıdaki daha başarılı olabilir olduğunu 5 denetleyicimin her birine bir bildirim için bir gözlemci ve bu uyarıyı oluşturmak ve sunmak için bir yöntem.

Yalnızca uyarıyı bir kez kurmanın ve sonra etkin durumda olan denetleyicide sunmanın bir yolu var mı? herhangi fikirler için

// I've got the following in each of my view controllers. 

// In viewDidLoad() 
override func viewDidLoad() { 
    super.viewDidLoad() 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SonglistViewController.presentSleepTimerFinishedAlert(_:)), name: "presentSleepTimerFinishedAlert", object: nil) 
} 


func presentTimerFinishedAlert(notification: NSNotification) { 
    let alertController = UIAlertController(title: "Timer finished", message: nil, preferredStyle: UIAlertControllerStyle.Alert) 
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
    presentViewController(alertController, animated: true, completion: nil) 
} 

Çok teşekkürler:

İşte benim kod!

cevap

1

Gezinme yığınının üstündeki ViewController'u bulabilir ve buradan doğrudan AlertController numarasını bulabilirsiniz. Eğer uygulamada herhangi bir yerden Üst ViewController bulmak için burada yayınlanan uzatma yöntemi kullanabilirsiniz:

https://stackoverflow.com/a/30858591/2754727

+0

dönmek için bir else if durumda eklemek gerekir, bir cazibe gibi çalışır! Çok teşekkürler! – nontomatic

1

Gerçekten navigasyon şema bağlıdır. Öncelikle mevcut VC'ye ihtiyacınız olacak. Gezinme denetleyicisi olarak root view denetleyiciniz varsa ve herhangi bir model göstermiyorsanız, geçerli VC'yi rootVC'den alabilirsiniz. Karışık navigasyonunuz varsa. yani, tabuta ve sonra da navigasyon kontrolörlerine, bazı modellerin onları oluşturmasıyla, AppDelegate üzerinde mevcut VC'yi arayacak ve döndürecek bir uzantı yazabilirsiniz.

Şimdi bu zamanlayıcı sınıfını bir yere sabitlemelisiniz - bu bir singleton olabilir veya yalnızca bir yere sabitlenebilir. Bu zamanlayıcı sınıfında, zamanlayıcı sona erdiğinde, geçerli VC'yi (AppDelegate'in uzantı yöntemini kullanarak veya kök gezinme denetleyicinize başvurarak) arayabilir ve üzerinde bir uyarı olabilir. Bununla

1
extension UIApplication { 
    /// The top most view controller 
    static var topMostViewController: UIViewController? { 
     return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController 
    } 
} 

extension UIViewController { 
    /// The visible view controller from a given view controller 
    var visibleViewController: UIViewController? { 
     if let navigationController = self as? UINavigationController { 
      return navigationController.topViewController?.visibleViewController 
     } else if let tabBarController = self as? UITabBarController { 
      return tabBarController.selectedViewController?.visibleViewController 
     } else if let presentedViewController = presentedViewController { 
      return presentedViewController.visibleViewController 
     } else { 
      return self 
     } 
    } 
} 

kolayca not etmek o kadar

UIApplication.topMostViewController?.present(alert, animated: true, completion: nil) 

Bir şey bir UIAlertController şu anda görüntülenen varsa, UIApplication.topMostViewController bir UIAlertController döneceğini gibi alert'ünüzü sunabilirsiniz. UIAlertController'un en üstünde sunulması tuhaf davranışlara sahiptir ve bundan kaçınılmalıdır. Bu nedenle, elle Sunumdan önce !(UIApplication.topMostViewController is UIAlertController) olmadığını kontrol edin veya mükemmel nil eğer self is UIAlertController

extension UIViewController { 
    /// The visible view controller from a given view controller 
    var visibleViewController: UIViewController? { 
     if let navigationController = self as? UINavigationController { 
      return navigationController.topViewController?.visibleViewController 
     } else if let tabBarController = self as? UITabBarController { 
      return tabBarController.selectedViewController?.visibleViewController 
     } else if let presentedViewController = presentedViewController { 
      return presentedViewController.visibleViewController 
     } else if self is UIAlertController { 
      return nil 
     } else { 
      return self 
     } 
    } 
}