【问题标题】:How to get UIAlertController over UIView in Objective C?如何在Objective C中通过UIView获取UIAlertController?
【发布时间】:2018-02-15 09:47:36
【问题描述】:

我是 iOS 编程新手, 我已经实现了 UITableViewController 并在我的主页上获得了 tableViewController 设计,就像一个弹出窗口一样, 背面我在主页和tableViewController之间添加了模糊效果。 模糊效果视图和 UITableView 都作为子视图添加到主屏幕中。

这是我的问题:

当我验证 tableViewController 的 UITextfields 时,alertController 出现在模糊效果和 tableViewController 的背面,我需要通过 tableViewController。

我怎样才能得到?

我在下面的代码中使用了模糊效果和tableView:

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];        
visualEffectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[[UIApplication sharedApplication].keyWindow addSubview:visualEffectView];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
viewaccept = (AcceptViewController *)[storyboard instantiateViewControllerWithIdentifier:@"AcceptViewController"];
[viewaccept willMoveToParentViewController:self];

CGRect screen = [[UIScreen mainScreen] bounds];
CGRect newFrame = CGRectMake(0,screen.size.height/4, self.view.frame.size.width, 350);
UIEdgeInsets insets = UIEdgeInsetsMake(0,10,0,10);

viewaccept.view.frame = UIEdgeInsetsInsetRect(newFrame,insets);
CGRect splitframe = viewaccept.view.frame;
[viewaccept.view setFrame:splitframe];
[[UIApplication sharedApplication].keyWindow addSubview:viewaccept.view];
[self addChildViewController:viewaccept];
[viewaccept didMoveToParentViewController:self];

我使用下面的代码来显示警报:

 NSString *message = @"Email Id is already registered";
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
 [self presentViewController:alert animated:YES completion:nil];

 int duration = 3; // duration in seconds
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
     [alert dismissViewControllerAnimated:YES completion:nil];
 });

【问题讨论】:

  • 你试过什么?分享您的代码
  • 试试这个:[self.view bringSubviewToFront:yourAlertController.view];
  • 我已经更新了我的代码..
  • 在当前呈现的UIViewController 即viewaccept 上呈现UIAlertController。

标签: ios objective-c uialertcontroller addsubview subviews


【解决方案1】:

试试这个

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
topWindow.rootViewController = [UIViewController new];
topWindow.windowLevel = UIWindowLevelAlert + 1;

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Your Message" preferredStyle:UIAlertControllerStyleAlert];

[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK",@"confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  // continue your work

// important to hide the window after work completed.
// this also keeps a reference to the window until the action is invoked.
topWindow.hidden = YES;
}]];

[topWindow makeKeyAndVisible];
[topWindow.rootViewController presentViewController:alert animated:YES completion:nil];

【讨论】:

  • 你好 Das,还有一个疑问,我已经为指纹实现了 Touchid,那个警报也有同样的问题,我该如何克服我的看法。
  • @Shubam Gupta。您能否就有关带有 touchid 的 AlertController 的问题提供更多描述。
【解决方案2】:

您可以在子控制器上显示您的 alertController,例如:

[viewaccept presentViewController:alert animated:YES completion:nil];

【讨论】:

  • 我已经尝试过,但我仍然在 uitabeviewcontroller 的背面保持警觉
  • 如果您将 tableViewController 添加为子控制器,而不是在子控制器而不是父控制器上显示 alertViewController。我的意思不是:[self presentViewController:alert animated:YES completion:nil];使用:[viewaccept presentViewController:alert animated:YES completion:nil];
  • @ShubamGupta - 获得顶级 VC 并展示您的警报
  • viewaccept 代码在 homepage.m 和 alertviewcontroller 出现是更平滑的文件,如 acceptviewcontroller.m
  • 您可以从导航堆栈或窗口中获取 topViewController 并在此显示您的警报
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多