【问题标题】:alertView from appDelegate does not switch view uppon buttonIndex来自 appDelegate 的 alertView 不会在 buttonIndex 上切换视图
【发布时间】:2012-09-23 16:38:56
【问题描述】:

我正在尝试从 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];
}
}

【问题讨论】:

  • 您的代码似乎运行良好。你确定代码正在执行吗?另外,你应该在你的 if 语句之前初始化 a (a = 0;)
  • 是的..介绍视图控制器没有呈现..任何想法为什么?

标签: ios xcode uialertview appdelegate


【解决方案1】:

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

您的问题是在您尝试呈现新的模态 ViewController 之前未设置 self.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 无关,因此您可能需要更改问题的标题和描述以使其更清楚,以供遇到类似问题的其他人使用。

编码愉快! :)

【讨论】:

  • 好吧..谢谢,但它不起作用。只是为了给你一个更清晰的模拟器图像,当显示警报视图时 firstViewController 已经在后台并且按下 buttonIndex 它没有切换到指定的 nib 也不能切换 rootViewController,因为 firstViewController 已经是 rootViewController.. 谢谢
  • 好的。你需要发布完整的代码,否则你会让人们盲目猜测。
  • 就是这样......我只是说输出是什么......如果我让你感到不舒服,我很抱歉......你可以用团队查看器检查它吗?如果你想请通过snakeye-cy@hotmail.com与我联系,以便我可以给你我的访问代码..谢谢
  • 要使您的代码正常工作,您必须在某处构造 self.viewController。我在上面的代码中看不到你在哪里做的。
  • introViewController *intro = [[introViewController alloc] initWithNibName:@"introViewController" bundle:nil]; [self.viewController presentModalViewController:intro animated:YES];它的最后两行..
【解决方案2】:

处理此问题的最佳方法是使用一些 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”。那是最好的方法。在您的示例中,在时间上使用的值。如果您关闭并重新启动应用程序,它会再次询问免责声明。

在您的代码中
一个;
使用
替换它 整数a = 0; 会好的。
谢谢。

【讨论】:

  • 感谢您的澄清,但我遇到的主要问题是没有推送适当的 nib 文件以供查看,我不知道为什么.. 有什么建议吗?
【解决方案3】:

如果你的代码和上面写的完全一样,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 中。这样,您可以检查用户是否首先启动了新版本。

【讨论】:

  • 是的,我对此没有任何问题,因为我的主要问题是特定的视图控制器(介绍)没有被推送到查看。请阅读我的完整描述
  • 啊,对不起。你检查过 self.viewcontroller 不是 nil 吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
相关资源
最近更新 更多