如何在ViewController之外获取应用程序接口?

时间:2015-06-29 11:08:22

标签: ios objective-c uialertview uialertcontroller uiapplication

我有两个文件.h和.m,有一些从viewcontrollers调用的util方法。我有一个带有警报视图,当点击确定按钮时,应用程序将终止。

用户点击按钮后,我将使用下一个代码退出:

[[UIApplication sharedApplication] terminateWithSuccess];

我有一个警告,告诉我UIApplication共享没有界面。

我的代码:

    #pragma mark - SHOW ALERTVIEW FOR IOS 7 or less AND IOS 8
   +(void) showQuitAlert:(NSString*)alertTitle withMessage:(NSString *)alertMessage{

        NSString *alertOkButtonText = @"Accept";

      if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( NSFoundationVersionNumber_iOS_8_0 ) ) {
              NSLog(@"iOS 8 dialog process");
             id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

          if([rootViewController isKindOfClass:[UINavigationController class]])
         {
            rootViewController = [((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
         }

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                             message:alertMessage
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                                   [[UIApplication sharedApplication] terminateWithSuccess];
                               }];


    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];
    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];

                             }];

    [alert addAction:ok];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];

    [alertController addAction:actionOk];
    [rootViewController presentViewController:alertController animated:YES completion:nil];
}

if (SYSTEM_VERSION_LESS_THAN(NSFoundationVersionNumber_iOS_7_0) ) {
    NSLog(@"iOS 7 or less dialog process");

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                        message:alertMessage
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:alertOkButtonText, nil];
    [alertView show];
   }

}

由于

1 个答案:

答案 0 :(得分:0)

您不应该在iOS中提供“退出应用”功能,例如在Android中。用户应点击主页按钮进入主屏幕。就这么简单。