在AppDelegate上显示条款和条件

时间:2016-03-01 10:52:40

标签: ios objective-c

我希望首先在UIAlertController上显示条件appdelegate,如果用户点击"同意",我可以转到我的viewController但是如果用户点击"取消",然后应用程序应保留在启动画面上。我有下面的代码,但它显示我的viewController没有uinavigationController,没有任何功能。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    userDefaults = [NSUserDefaults standardUserDefaults];
    if (![userDefaults boolForKey:@"TermsAndConditions"]) {

        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.rootViewController = [[UIViewController alloc] init];
        _window.windowLevel = UIWindowLevelAlert;
        _window.backgroundColor = [UIColor prosenseWhiteColor];

        UIImageView *windowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Defaulte"]];
        [windowImage setFrame:[UIScreen mainScreen].bounds];
        [windowImage setContentMode:UIViewContentModeScaleAspectFit];
        [_window addSubview:windowImage];

        [_window makeKeyAndVisible];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"termsAndConditions" ofType:@"txt"];
        NSString *tsAndCs = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

        UIAlertController *termsAndConditionsController = [UIAlertController alertControllerWithTitle:@"Terms and conditions" message:tsAndCs preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *agree = [UIAlertAction actionWithTitle:@"Agree" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

            [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];

            [userDefaults setBool:YES forKey:@"TermsAndConditions"];
            [userDefaults synchronize];

                        UIStoryboard *st_board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        PTPdfFileListViewController *pdfFileListView = (PTPdfFileListViewController*)[st_board instantiateViewControllerWithIdentifier:@"PTPdfFileListViewController"];
                        self.window.windowLevel = UIWindowLevelNormal;
                        [self.window makeKeyAndVisible];
                        [(UINavigationController*)self.window.rootViewController presentViewController:pdfFileListView animated:NO completion:nil];
        }];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
            NSLog(@"Cancel");
        }];

        [termsAndConditionsController addAction:cancel];
        [termsAndConditionsController addAction:agree];
        [_window.rootViewController presentViewController:termsAndConditionsController animated:YES completion:nil];

    }else {
        NSLog(@"accepted terms already");
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

你应该在UINavigationController中以root身份嵌入PTPdfFileListViewController并呈现UINavigationController。

答案 1 :(得分:0)

尝试使用pushViewController代替presentViewController,然后它将位于主应用程序的导航控制器中。

用户可以返回,所以我不确定这是您正在寻找的解决方案。

没有后退按钮的解决方案:

[(UINavigationController*)self.window.rootViewController setViewControllers:@[pdfFileListView] animated:NO];