以类似于Windows的MessageBox()的方式使用UIAlertView吗? [英] Using UIAlertView in a manner similar to Windows' MessageBox()?

查看:59
本文介绍了以类似于Windows的MessageBox()的方式使用UIAlertView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone的新手,我希望能够以类似于Windows MessageBox()Delphi中的MessageDlg()的方式使用UIAlertView.

I'm new to the iPhone and I would like to be able to use UIAlertView in a manner similar to the Windows MessageBox() or the MessageDlg() in Delphi.

例如,我有一种方法需要让用户确认某些内容,然后根据他们的回答进行操作.

For example, I have a method that needs to ask the user for confirmation on something, and proceed based on their response.

例如(伪代码):

-(void)doSomething
{
  [doStep1];
  [doStep2];
  var myValue = [getDefaultValue];

  if (myValue = nil)
  {
    if [promptUser(@"No value in setting. Use the default value?")] //UIAlertView here?
    {
       myValue = @"defaultValue";
    }
    else
      return;  // bug out of the routine 'cause we have no value.
  }

  [doStep3 withValue:myValue];

}

或者,换一种说法-有没有一种方法可以使用UIAlertView在例程中向用户询问问题,以控制该例程的逻辑流程?

Or, put put it another way- is there a way of using UIAlertView to ask the user a question within a routine as a way of controlling the logic flow of that routine?

推荐答案

我不知道MessageDlg()是什么,但是您可以肯定UIAlertView的子类并根据按下的按钮来处理对话框响应,例如:

I have no idea what MessageDlg() is, but you can certainly subclass UIAlertView and handle the dialog response, based on which button is pressed, e.g.:

设置UIAlertView子类标题:

//
//  ARReachabilityAlertView.h
//

#import <UIKit/UIKit.h>

@interface ARReachabilityAlertView : UIAlertView <UIAlertViewDelegate> {
}

@end

设置UIAlertView子类实现:

//
//  ARReachabilityAlertView.m
//

#import "ARReachabilityAlertView.h"

@implementation ARReachabilityAlertView

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    [self setTitle:@"Error"];
    [self setMessage:@"This application won't run without a network connection. Do you want to quit?"];
    [self addButtonWithTitle:@"Quit"];
    [self addButtonWithTitle:@"Continue"];
    [self setDelegate:self];
  }
  return self;
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0)
    exit(0); // quit application if "Quit" is pressed; otherwise, do nothing
}

- (void) drawRect:(CGRect)rect {
  [super drawRect:rect];
}

- (void) dealloc {
  [super dealloc];
}

@end

请注意alertView:clickedButtonAtIndex:委托方法.这将处理您用来决定应用程序进行方式的条件.您可以从此处发送NSNotification,也可以在应用程序委托中调用任何方法.

Note the alertView:clickedButtonAtIndex: delegate method. This handles the conditionals you use to decide how the application proceeds. You can send an NSNotification from here, or call a method in the application delegate, whatever you want.

在我的示例中,如果没有网络连接,则实例化此UIAlertView;如果用户在警报视图中单击退出",则关闭应用程序.否则,如果用户单击继续",则应用程序将照常运行.

In my example, this UIAlertView is instantiated if there is no network connection, and the application is closed if the user clicks "Quit" in the alert view. Otherwise, if the user clicks "Continue" the application keeps running as usual.

请注意,子类的实现需要调用drawRect:方法.我不确定这是否是错误,因为我希望在超类中调用drawRect:方法.我就此向Apple提交了错误报告,但是我什么也没听到.如果您想查看效果,请注释掉它-这很有趣.

Note that the implementation of the subclass requires the drawRect: method be called. I'm not sure if this is a bug or not, since I'd expect the drawRect: method to be called in the super-class; I filed a bug report with Apple on this one, but I haven't heard anything. Comment it out if you want to see what the effect will be — it's kind of interesting.

这篇关于以类似于Windows的MessageBox()的方式使用UIAlertView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆