gpt4 book ai didi

ios - AlertViewController - 其 View 不在窗口层次结构中问题

转载 作者:行者123 更新时间:2023-12-01 18:54:58 25 4
gpt4 key购买 nike

在下面的代码中,我试图仅在发生特定事件时调用方法 handleEvent。为此,我发布了事件发生的通知(未在下面的代码中显示),以便观察者和 handleEvent 观察到通知(如下面的代码中所示)。

我想要一个警报 viewController 弹出并获取文本输入,以便我可以接收它。
但我收到以下错误:

Warning: Attempt to present <UIAlertController: 0x7f9d1347ed90> on <MSMDemoFirstViewController: 0x7f9d1347e920> whose view is not in the window hierarchy!

尽管我使用的是单例,但我无法理解,但我无法正确获取 alertViewController。任何帮助都会很有用。
   #import "MSMDemoFirstViewController.h"
#import "CAMDOReporter.h"
static MSMDemoFirstViewController * instance = nil;
@interface MSMDemoFirstViewController ()

@end

@implementation MSMDemoFirstViewController

+ (MSMDemoFirstViewController *)sharedInstance {
// Singleton implementation
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (instance == nil) {
instance = [[MSMDemoFirstViewController alloc] init];
}
});
return instance;
}

+(void)load{
[[NSNotificationCenter defaultCenter] addObserver:[MSMDemoFirstViewController sharedInstance] selector:@selector(handleEvent:) name:EVENT_OCCURRED object:nil];
}


+(void) initialize{

}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//[[CAMDOReporter sharedInstance] startApplicationTransaction:@"DemoEventApplicationTransaction"];
// [self showAlert];
}

-(void)showAlert{
NSLog(@"2ew");
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// If you need to customize the text field, you can do so here.
}];

// Create the action.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The simple alert's cancel action occured.");
}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Text Entry\" alert's other action occured.");
}];
// Add the action.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}

-(void)handleEvent:(NSNotification *)note{
NSLog(@"Handle Crash Event");
NSDictionary *crashData = [note userInfo];
if(crashData !=nil){
//Implement any UI Logic to get any response from the user. e.g. AlertView, TextFields.
//Once the feedback is received convert it into NSString and send the data.
// Show a text entry alert with two custom buttons.
[[MSMDemoFirstViewController sharedInstance] showAlert];
NSString *feedback=[[NSUserDefaults standardUserDefaults] objectForKey:@"crashDetails"];
[[CAMDOReporter sharedInstance] setCustomerFeedback:feedback];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

现在我得到的输出是:
2015-01-21 20:17:45.762 CAMAADemo[18891:1558312] Handle Crash Event
2015-01-21 20:17:45:762 CAMAADemo[18891:1807] Posting Notification about crash
2015-01-21 20:17:45.762 CAMAADemo[18891:1558312] 2ew
2015-01-21 20:17:45.778 CAMAADemo[18891:1558312] Warning: Attempt to present <UIAlertController: 0x7ff5d2273580> on <MSMDemoFirstViewController: 0x7ff5d2204be0> whose view is not in the window hierarchy!

这意味着调用将使用 showAlert 方法,但是尽管我得到了同样的错误。

即使下面的代码也无济于事。
-(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCrashEvent:) name:CAMAA_CRASH_OCCURRED object:nil];
}
-(void)showAlert{
NSLog(@"2ew");
NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// If you need to customize the text field, you can do so here.
}];

// Create the action.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The simple alert's cancel action occured.");
}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Text Entry\" alert's other action occured.");
}];
// Add the action.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}

-(void)handleCrashEvent:(NSNotification *)note{
NSLog(@"Handle Crash Event");
NSDictionary *crashData = [note userInfo];
if(crashData !=nil){
//Implement any UI Logic to get any response from the user. e.g. AlertView, TextFields.
//Once the feedback is received convert it into NSString and send the data.
// Show a text entry alert with two custom buttons.
[self showAlert];
NSString *feedback=[[NSUserDefaults standardUserDefaults] objectForKey:@"crashDetails"];
[[CAMDOReporter sharedInstance] setCustomerFeedback:feedback];
}
}

最佳答案

您的代码的问题是您创建了一个全新的 View Controller 并显示来自新 Controller 的警报。但是该 Controller 尚未显示,因此出现错误消息。

修复此代码的正确方法是显示来自实际呈现的 View Controller 的警报。

为此,将实际的 View Controller 实例注册为通知处理程序,而不是在 load 中注册类。方法。

进行以下更改:

  • 消除 load方法。
  • 将电话移至 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEvent:) name:EVENT_OCCURRED object:nil];viewDidLoad方法。
  • 更改handleEvent:成为一个实例方法。
  • 更改[[[self alloc]init] showAlert];[self showAlert]; .
  • 添加 dealloc方法并注销通知观察者。
  • 关于ios - AlertViewController - 其 View 不在窗口层次结构中问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081097/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com