dbettermann/DBAlertController
Fork: 19 Star: 58 (更新于 2024-12-08 02:24:12)
license: MIT
Language: Swift .
A UIAlertController subclass that's independent of the view controller it's presented from. i.e. Adding UIAlertView's show() to UIAlertController.
最后发布版本: 0.3.0 ( 2016-10-02 08:21:22)
DBAlertController
Adding UIAlertView's show() functionality to UIAlertController.
Background
Have you ever needed to present an alert without knowing the visible view controller? In case you're drawing a blank, let me give an example.
Example - App Updates
Assume we have an app in the App Store and we want to let the users know there is an update. How would we do that?
iOS 7 and earlier - UIAlertView
AppDelegate.swift
func applicationDidBecomeActive(application: UIApplication) {
if update {
UIAlertView(title: "Update Available", message: "Would you like to update your app?", delegate: self, cancelButtonTitle: "Yes!").show()
}
}
Easy, right? We don't have to worry about which view controller is visible. UIAlertView seems to find the visible view controller and add itself as a subview.
UIAlertView and system alerts might actually use their own UIWindow just like DBAlertController, but I digress.
iOS 8 and newer - UIAlertController
AppDelegate.swift
func applicationDidBecomeActive(application: UIApplication) {
if update {
let alertController = UIAlertController(title: "Update Available", message: "Would you like to update your app?", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Yes!", style: .Default, handler: nil))
window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
This will work... but only if the rootViewController's view is in the window hierarchy.
What if the rootViewController's view is not in the window hierarchy?
- The UIAlertController does not appear.
- You get a warning message written to your console. Something like:
015-05-13 08:09:58.923 DBAlertController[1002:15635] Warning: Attempt to present <UIAlertController: 0x7ff5f2e3a500> on <UINavigationController: 0x7ff5f2f24760> whose view is not in the window hierarchy!
There's a demo of this scenario in the source. Pull it down and try it yourself.
Fixing the issue
DBAlertController fixes the issue above by presenting the UIAlertController on its own UIWindow.
Installation
Cocoapods
Add pod 'DBAlertController'
to your Podfile.
Carthage
Add github "dbettermann/DBAlertController"
to your Cartfile.
Manual
Copy and paste DBAlertController.swift into your project.
Usage
let alertController = DBAlertController(title: "DBAlertController", message: "Hello World!", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
alertController.show()
Here is the full signature for show()
:
func show(animated flag: Bool = true, completion: (() -> Void)? = nil)
As you can see, you can also pass an animated flag and completion closure that get passed along to
presentViewController(alertController, animated: flag, completion: completion)
Feedback
All feedback is welcome. Tweet at me or send me an email with any questions, comments, or issues.
最近版本更新:(数据更新于 2024-09-15 04:45:11)
2016-10-02 08:21:22 0.3.0
2016-04-04 09:29:36 0.2.5
2015-12-09 23:44:29 0.2.2
dbettermann/DBAlertController同语言 Swift最近更新仓库
2024-12-21 13:48:53 argmaxinc/WhisperKit
2024-12-19 06:06:30 stripe/stripe-ios
2024-12-16 23:43:56 wordpress-mobile/WordPress-iOS
2024-12-04 20:33:05 Artificial-Pancreas/iAPS
2024-12-03 10:12:09 utmapp/UTM
2024-11-25 23:11:48 Alamofire/Alamofire