2

我创建了一个通用方法方法来显示警报视图。我已附加我的代码以供审查。我正在尝试验证三个 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] 下方添加了以下代码: // Block for 5 seconds [NSThread sleepForTimeInterval:5.0];

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

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

再次感谢您的帮助,任何想法将不胜感激。

4

2 回答 2

2

你确定这是在主线程上运行的吗?在调用 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
}
于 2012-09-25T21:24:51.320 回答
0

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

@interface ViewController : UIViewController <UIAlertViewDelegate>

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

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

希望这可以帮助。

于 2012-09-25T18:53:40.650 回答