来自appDelegate的uialertView不会切换按钮索引上的视图

时间:2012-09-23 16:38:57

标签: ios xcode uialertview appdelegate

我正在尝试从appDelegate创建一个alertView但我无法让它工作。当应用程序首次启动时,alertView将充当免责声明。我无法让它工作.. introViewcontroller没有出现。我究竟做错了什么? PS。我使用的故事板除了作为一个nib文件的intoViewController。我的根视图控制器是一个“fisrtViewController”这里是我的代码:谢谢..

int a;

和didFinishLaunchingWithOptions

    if ( a == 0) {

UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:@"Read Before use" message:@"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"No!" otherButtonTitles:@"Yes Let me In", nil];

[disclaimer show];
}


// Override point for customization after application launch.
return YES;
}

-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
  //  FirstViewController *firstView = [[FirstViewController alloc] init];
    a+=1;
  //  [self.viewController presentModalViewController:firstView animated:YES];
}
else if (buttonIndex == 2) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"You are not allowed to use this app due to the fact that you did not agree to the terms and Conditions. Please exit this app!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

if (buttonIndex ==1) {
       introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil];
       [self.viewController presentModalViewController:intro animated:YES];
   introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil];
   [self.viewController presentModalViewController:intro animated:YES];
}
}

3 个答案:

答案 0 :(得分:1)

处理此问题的最佳方法是使用一些NSString值写入NSUserDefaults文件。当应用程序第一次启动时接受免责声明(例如:@“accept”)如果用户不接受也相应地更新了NSString值。(@“notAccept”)。

在didFinishLaunchingWithOptions

if (![[[NSUserDefaults standardUserDefaults]valueForKey:@"key_disclaimer"] isEqualToString:@"accepted"]) {  
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:@"Read Before use" message:@"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"No!" otherButtonTitles:@"Yes Let me In", nil];   
[disclaimer show];
[disclaimer release];  
//        make sure to release it.  
}

//如果用户接受免责声明 [[NSUserDefaults standardUserDefaults] setValue:@“accepted”forKey:@“key_disclaimer”];
//如果用户不接受免责声明, [[NSUserDefaults standardUserDefaults] setValue:@“notAccepted”forKey:@“key_disclaimer”];

相应地更新“key_disclaimer”。这是最好的方式。在你的例子中,在时间上使用的值。如果您关闭并重新启动应用程序,它将再次询问免责声明。

在你的代码中 int a;
使用
替换它 int a = 0; 没关系。
感谢名单。

答案 1 :(得分:1)

如果您的代码与您上面的代码完全相同,则a不会为0。 如果在启动时未将其显式设置为0,则它​​可能具有任何值。

如果要在安装后第一次检查应用是否已打开,请使用NSUserDefaults 例如(在didFinishWithOptions中):

int startCount=[[NSUserDefaults standardUserDefaults] intForKey:@"startCount"];
if (startCount==0]){
  //Is the first start, show your agreement
  //Then increase startCount so that this will not be called at next start
  [[NSUserDefaults standardUserDefaults] setInteger:startCount++ forKey:@"startCount"];
} 

您还可以将您的版本号存储在NSUserDefaults中。这样,您可以检查用户是否首次启动新版本。

答案 2 :(得分:1)

啊......现在我明白你要做什么了。

问题是在尝试呈现新的模态ViewController之前未设置self.viewController。另外,我认为你的buttonIndex是一个。

尝试改为:

if (buttonIndex == 1)
    {
        _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil];
        _window.rootViewController = _intro;
        [_window makeKeyAndVisible];
    }

由于您的问题与UIAlertView没有任何关系,因此您可能希望更改问题的标题和说明,以便更清楚地解决问题。

快乐的编码! :)

相关问题