我有一个Obj C应用程序,其中AppDelegate包含一个切换UIAlertController的方法(一个'正在加载'没有OK / Cancel按钮的警报)。显示警报工作,但隐藏不。有人可以建议吗?
-(void)toggleAlert(show){
UIViewController *vc = self.window.rootViewController;
if (show) {
// Show alert
[vc presentViewController:self.loadingAlert animated:YES completion:nil];
} else {
// Hide alert
[self.loadingAlert dismissViewControllerAnimated:YES completion:nil];
}
}
答案 0 :(得分:0)
用
替换隐藏线[self dismissViewControllerAnimated:YES completion:nil];
答案 1 :(得分:0)
您需要在appDelegate中发送当前的ViewController,并且需要向其发送警报。
<强> AppDelegate.h 强>
@property (strong, nonatomic)UIAlertController * alert;
-(void)showAlert:(NSString *)strMessage viewController:(UIViewController*)VC;
<强> AppDelegate.m 强>
-(void)showAlert:(NSString *)strMessage viewController:(UIViewController*)VC{
_alert = [UIAlertController
alertControllerWithTitle:@"Title"
message:strMessage
preferredStyle:UIAlertControllerStyleAlert];
//Add Buttons
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
//Add your buttons to alert controller
[_alert addAction:yesButton];
[_alert addAction:noButton];
[VC presentViewController:_alert animated:YES completion:nil];
}
如果您想在某个时间以编程方式隐藏此提醒,请使用以下方法:
-(void)hideAlert
{
[_alert dismissViewControllerAnimated:YES completion:^{
}];
}
答案 2 :(得分:0)
您可以使用此类别
#import "UIAlertController+Window.h"
#import <objc/runtime.h>
@interface UIAlertController (Window)
- (void)show;
- (void)show:(BOOL)animated;
@end
@interface UIAlertController (Private)
@property (nonatomic, strong) UIWindow *alertWindow;
@end
@implementation UIAlertController (Private)
@dynamic alertWindow;
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, @selector(alertWindow));
}
@end
@implementation UIAlertController (Window)
- (void)show {
[self show:YES];
}
- (void)show:(BOOL)animated {
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
// Applications that does not load with UIMainStoryboardFile might not have a window property:
if ([delegate respondsToSelector:@selector(window)]) {
// we inherit the main window's tintColor
self.alertWindow.tintColor = delegate.window.tintColor;
}
// window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard)
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
self.alertWindow.windowLevel = topWindow.windowLevel + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// precaution to insure window gets destroyed
self.alertWindow.hidden = YES;
self.alertWindow = nil;
}
@end
样本用法:
// need local variable for TextField to prevent retain cycle of Alert otherwise UIWindow
// would not disappear after the Alert was dismissed
__block UITextField *localTextField;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Global Alert" message:@"Enter some text" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"do something with text:%@", localTextField.text);
// do NOT use alert.textfields or otherwise reference the alert in the block. Will cause retain cycle
}]];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
localTextField = textField;
}];
[alert show];
答案 3 :(得分:0)
试试这个:
AlertViewController和ActionSheet的解决方案
-(void)showAlertWithMessage:(NSString *)message WithTitle:(NSString *)title WithCancelButtonTitle:(NSString *)cancelTitle WithAnotherButtonTitle:(NSString *)anotherTitle WithAlertStyle:(UIAlertControllerStyle)alertStyle WithCallBack:(void(^) (BOOL isConfirm))alertCallback{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:Localization(title)
message:Localization(message)
preferredStyle:alertStyle];
if (anotherTitle){
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:Localization(anotherTitle)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
if (alertCallback) {
alertCallback(YES);
}
}];
[alert addAction:yesButton];
}
if (cancelTitle) {
UIAlertAction* cancelButton = [UIAlertAction
actionWithTitle:Localization(cancelTitle)
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
if (alertCallback) {
alertCallback(NO);
}
}];
[alert addAction:cancelButton];
}
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[[vc presentedViewController] ? vc.presentedViewController : vc presentViewController:alert animated:YES completion:^{
}];
}
<强>调用强>
[appDelegateIntsance showAlertWithMessage:message WithTitle:title WithCancelButtonTitle:@"Cancel" WithAnotherButtonTitle:@"OK" WithAlertStyle:UIAlertControllerStyleAlert WithCallBack:^(BOOL isConfirm) {
// handal you click event here
}];