【问题标题】:UIAlertView does not display on iphoneUIAlertView 不显示在 iPhone 上
【发布时间】:2012-09-28 09:33:28
【问题描述】:

我创建了一个通用方法方法来显示警报视图。我已附加我的代码以供审查。 我正在尝试验证三个 uitextfields,如果无效,请将相关信息放入 uialertview 方法中使用的实例变量中。您可以看到我在代码中散布了大量的 nslog 语句。方法开头和结尾的 nslog 都显示发生的错误的相关信息。当我点击模拟器屏幕上的按钮时,它不会显示任何消息,但屏幕背景是浅灰色的,会变成深灰色。

代码如下:

- (void)SendErrorMessage
{
    NSLog(@"Starting SendErrorMessage");
    NSLog(@"initWithTitle = %@", self.alertViewTitleText);
    NSLog(@"message = %@", self.alertViewMessageText);
    NSLog(@"cancelButtonTitle = %@", self.alertViewCancelButtonText);

    UIAlertView *alert = [[UIAlertView alloc]
                         initWithTitle:self.alertViewTitleText
                         message:self.alertViewMessageText
                         delegate:nil cancelButtonTitle:
                         self.alertViewCancelButtonText
                         otherButtonTitles:nil];
    [alert show];
    NSLog(@"Finished SendErrorMessage");
    return;

}

对此的任何帮助将不胜感激。

好的,

我现在真的很困惑。我进入代码并在 [alert show] 下方添加了以下代码: // 阻塞 5 秒 [NSThread sleepForTimeInterval:5.0];

我这样做是为了查看 alertview 是否真的被快速显示。不,根据我的日志,程序坐了 5 秒钟,然后继续执行程序逻辑。我还替换了uialertview 一个 uiactionsheet 以查看它是否会显示 uialertview 不显示的位置。再一次,不,它们都不会显示在模拟器屏幕上。

这是我的 .h 文件中的接口语句:
@interfaceBeginningScene:UIViewController, (小于)UIActionSheetDelegate, UITextViewDelegate, UIAlertViewDelegate(大于)

再次感谢您的帮助,我们将不胜感激。

【问题讨论】:

  • 你能打印出你的 NSLogs
  • 您的应用程序此时会崩溃吗?警报视图显示在半透明背景上(屏幕中间较亮),因此听起来好像显示了此背景,但实际警报视图本身并未显示,这可能是因为应用程序在该点崩溃.
  • 你确定这个方法是从主线程调用的吗?无论如何,您可能希望将代码的 UIAlert 部分放在主线程上运行,以便可以从后台线程调用该方法并且警报仍然有效。
  • 从真正简单的开始。在最前面的视图控制器中尝试显示“Hello World”警报。如果可行,那么在视图控制器的同一点尝试您的课程。这会告诉你问题出在哪里。

标签: objective-c ios uialertview


【解决方案1】:

你确定这是在主线程上运行的吗? 在调用 sendErrorMessage 时尝试使用它

[self performSelectorOnMainThread:@selector(sendErrorMessage)];

跳过return语句,没用。

不管怎样,试试这段代码并将方法也复制到你的头文件中,注意小写的 S,只是一个偏好。

- (void)sendErrorMessage
{
    UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Title"
                      message:@"message"
                      delegate:nil cancelButtonTitle:
                      @"OK"
                      otherButtonTitles:nil];
[alert show];
[alert release]; // if you are not using ARC
}

【讨论】:

  • 我的印象是,当执行 uialertview 时,它会暂停应用程序的执行,直到用户输入回复。情况似乎并非如此。该应用程序立即对另一个场景进行转场并异常终止。在应用异常结束之前,可能应用没有时间完成 uialertview。
【解决方案2】:

您可以尝试在将显示 UIAlertView(.h 头文件)的类中使用以下内容:

@interface ViewController : UIViewController <UIAlertViewDelegate>

然后,在 .m 实现中,在创建 UIAlertView 时将委托设置为 self

    UIAlertView *alert = [[UIAlertView alloc]
                     initWithTitle:self.alertViewTitleText
                     message:self.alertViewMessageText
                     delegate:self cancelButtonTitle:
                     self.alertViewCancelButtonText
                     otherButtonTitles:nil];

希望这会有所帮助。

【讨论】:

  • 我最近发现 uialertview 在用户响应之前不会暂停应用程序。执行 uialertview 后,程序立即执行 segue 到另一个场景并异常终止。可能是该应用没有时间在应用异常结束之前完成 uialertview。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多