减少警报视图编码

Arp*_*ekh 1 iphone cocoa-touch objective-c ios xcode6

所有.在我的iOS应用程序中.在我的许多页面上Too many alerts and also with many Network conditions.

有太多警报文本,我厌倦了.每次我必须使用相同的代码.

我可以在某个帮助程序类中声明此代码吗?或者重复使用此代码?

-(BOOL)checkInternetAndlocationServices {

    if(IS_INTERNET) {
        if([CLLocationManager locationServicesEnabled] &&
           [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied){
            return YES;
        }
        else
        {
            NSLog(@"Location services are disabled.");
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Location services are off." message:@"This app requires an Location services." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Location Services", nil];
            [alert setTag:NO_LOCATIONSERVICES];
            [alert show];
            return NO;
        }
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Internet connection is off." message:@"This app requires an internet connection and locations services, please enable internet connection and location services." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
        [alert setTag:NO_INTERNET];
        [alert show];
        return NO;
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.如果您发现它有用,请编辑此问题.

感谢您提供良好的方法,还有其他任何方式,最受欢迎的例子.

Abu*_*Dar 5

  1. 您可以创建一个帮助器类并使用类方法来显示警报.
  2. 您还可以创建UIAlertView类别并创建用于显示警报的类方法.(编辑)
     
    @implementation UIAlertView (MyAlert)
    +(void) showAlertWithTitle:(NSString *)title message:(NSString *)message {
    [[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
    @end
    
  3. 您可以在.pch文件或某个帮助程序头文件中定义宏以显示警报.
    #define Alert(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]
    Alert(@"This is Title",@"This is message",self);