0

我正在尝试从 appDelegate 创建一个 alertView,但我无法让它工作。当应用程序首次启动时,alertView 将作为免责声明。我无法让它工作.. introViewcontroller 没有出现。我究竟做错了什么?附言。我使用情节提要,除了 intoViewController 这是一个 nib 文件。我的根视图控制器是“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];
}
}
4

3 回答 3

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 替换它;会没事的。
谢谢。

于 2012-09-23T17:46:20.773 回答
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 中。这样,您可以检查用户是否首先启动了新版本。

于 2012-09-23T17:46:26.830 回答
1

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

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

请尝试:

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

由于您的问题与 无关UIAlertView,因此您可能希望更改问题的标题和描述以使其更清楚,以供其他遇到类似问题的人使用。

快乐编码!:)

于 2012-09-23T21:28:18.430 回答