创建一个显示UIAlertView的实用工具类

时间:2011-05-18 14:10:22

标签: iphone objective-c ios uialertview code-design

我有这个警告:

  

没有可用的互联网连接,请再试一次。

我需要很多可以产生此消息的代码块,并且我想将UIAlertView放在一个我不会每次都创建它的类中,这可能吗?

2 个答案:

答案 0 :(得分:8)

用于ActionGeneric类的文件

#import <Foundation/Foundation.h>

@interface ActionGeneric : NSObject {

}

+(void)showAlert;
@end

m file

    #import "ActionGeneric.h"

    @implementation ActionGeneric

     +(void)showAlert{
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"haveInternetConnection",@"") 
                                                      message:@"" 
                                                     delegate:nil 
                                            cancelButtonTitle:NSLocalizedString(@"kOk",@"") 
                                            otherButtonTitles:nil];
      [alert show];
      [alert release];
    }
    @end

然后你只需导入动作泛型并调用它

[ActionGeneric showAllert];

您应该查看Learning Objective-C

答案 1 :(得分:2)

当然可以。你可以使它成为你的类的类方法,这样你就不需要它的任何实例。然后你可以打电话给:

[InternetAlert displayAlert];

或类似的东西。