我有working iOS application
为了support iOS8
,我正在取代UIAlertView/UIActionSheet with
UIAlertController
。
问题:
为了显示UIAlertController,我需要UIViewController类的presentViewController方法。
但是UIAlertView是来自inherited
的UIView or NSObject
类,
由于显而易见的原因,我无法得到[self presentViewController...]
方法。
我的工作 : 我尝试获取rootViewController表单当前窗口并显示UIAlertController。
[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController ...]
但有一些旋转问题,如果我的当前视图控制器没有旋转支持,它将旋转,如果UIAlertController打开。
题 : 有没有人遇到同样的问题,并有安全的解决方案? 如果是,请给我一些例子或给一些指导
看起来您当前(iOS8之前)从视图对象中触发警报视图。这是非常糟糕的做法,因为一般警报应该从动作和逻辑中触发。并且该代码应该存在于控制器中。
我建议你重构当前代码,将触发警报的逻辑移动到正确的控制器,然后通过使用self
作为控制器,您可以轻松升级到iOS 8。
相反,如果您从外部对象调用警报,则将控制器传递给调用警报的方法。在上游的某个地方,您必须了解控制器。
我知道这个问题已经得到了回答......但是我也在寻找同样的问题,但上述解决方案都不适合我。
因此,经过多次尝试和错误,我找到了一个非常简单和可持续的解决方案。
func showError(title: String?, error: String?) {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
CommonMethods.instance.topMostController()?.present(alert, animated: true, completion: nil)
})
}
static let instance = CommonMethods()
fileprivate func topMostController() -> UIViewController? {
var presentedVC = UIApplication.shared.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController {
presentedVC = pVC
}
if presentedVC == nil { }
return presentedVC
}
我今天解决了一个基本相似的问题。和Jageen一样,我遇到了一个我想要呈现UIAlertController但是来自非UIViewController类的情况。就我而言,我希望在运行HTTP请求的故障块时弹出警报。
这就是我使用的,不像我们的朋友,这对我来说非常完美。
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(errorAlert, animated: true, completion: nil)
UIView
课程的更好解决方案如下
的ObjectiveC
UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController.........
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
return topVC;
}
迅速
var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil){
topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
我的解决方案如下:
迅速
class alert {
func msg(message: String, title: String = "")
{
let alertView = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil)
}
}
以下是示例用法:
let Alert = alert()
Alert.msg("My alert (without title)")
Alert.msg("This is my alert", title: "Warning!")
我有一种情况,子视图包含一个关闭它的按钮。我提出警报以确认行动。它向委托发送消息 - 这是包含子视图的视图控制器 - 以删除子视图
最初我从UIView呈现了一个UIAlertView。重构UIAlertController,因为UIAlertController不能像UIAlertView那样呈现自己,我想出了以下内容(在Swift中;很容易翻译成ObjC):
将协议添加到子视图:
protocol MySubviewDelegate {
// called when user taps subview/delete button
// or, you could call it from a gesture handler, etc.
func displayAlert(alert : UIAlertController)
// called when user confirms delete from the alert controller
func shouldRemoveSubview(sender : AnyObject)
}
为子视图添加委托,并为按钮/手势点击添加处理程序:
class MySubview : UIView {
var subviewDelegate : MySubviewDelegate!
...
func handleTap(sender : AnyObject) {
// set up the alert controller here
var alert = UIAlertController(title: "Confirm Delete",
message: "This action is permanent. Do you wish to continue?",
preferredStyle: UIAlertControllerStyle.Alert)
// Cancel action
// nil handler means "no action if Cancel button selected"
alert.addAction(UIAlertAction(title: "Cancel",
style: UIAlertActionStyle.Cancel,
handler: nil))
// Confirm action
alert.addAction(UIAlertAction(title: "Confirm",
style: UIAlertActionStyle.Default,
handler: { (action : UIAlertAction!) -> Void in
// call delegate method to perform confirmed action, - i.e. remove
self.subviewDelegate.shouldRemoveSubview(self)
}))
// call delegate method to display alert controller
// send alert object to delegate
self.subviewDelegate.displayAlert(alert)
}
}
将调用UIViewController设置为子视图的委托,例如,在其viewDidLoad()方法中,并包括协议方法:
class viewController : UIViewController, MySubviewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.subviewDelegate = self
...
}
func displayAlert(alert : UIAlertController) {
presentViewController(alert, animated: true, completion: nil)
}
func shouldRemoveSubview(sender : AnyObject) {
// cast as UIView / MySubview subclass
var subview = sender as MySubview
// remove the subview / perform the desired action
subview.removeFromSuperview()
...
}
...
}
这样就无需找到最顶层的视图控制器,或者将对视图控制器的引用传递给子视图(除了在对象/委托关系中)。
在Swift 3中:
UIApplication.shared.keyWindow?.rootViewController?.present(alertView, animated: true, completion: nil)
适用于Swift 4及以上版本
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
对于NSObject类中的显示UIAlertController,请使用Code下面的代码。
UIAlertController * popup = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[popup dismissViewControllerAnimated:YES completion:nil];
}];
[popup addAction:cancel];
UIViewController *rootViewController = [[Helper shareInstance] topViewController];
[rootViewController presentViewController:popup animated:YES completion:nil];
//将下面的方法放在全局助手类中。
- (UIViewController *)topViewController {
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController {
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
通常,应在视图控制器中处理警报。以下是所需代码的示例:
斯威夫特3
private func displayError(message: String) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
let okayAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
alertController.addAction(okayAction)
present(alertController, animated: true, completion: nil)
}