我的代码需要一些帮助。我有一个 UIAlert 会在您第一次打开应用程序时弹出,在该弹出窗口中我有两个按钮,用户将选择其中一个。我希望应用程序记住用户选择哪个按钮来执行某些代码或其他。问题是我在这里有这段代码:
-(void)changeLabel{
progressView.progress += 0.25;
scan.hidden = YES;
if (progressView.progress == 1 ) {
label.hidden = YES;
progressView.hidden = YES;
[timer invalidate];
imagesText.hidden = NO;
int randomNumber = arc4random() % 4;
switch (randomNumber) {
case 0:
imagesText.image = [UIImage imageNamed:@"image1.png"];
break;
case 1:
imagesText.image = [UIImage imageNamed:@"image2.png"];
break;
case 2:
imagesText.image = [UIImage imageNamed:@"image3.png"];
break;
case 3:
imagesText.image = [UIImage imageNamed:@"image4.png"];
default:
break;
}
}
}
所以我想以某种方式做到这一点,如果用户选择了第一个按钮,应用程序将在案例 0,1 和 2 之间进行切换,如果他选择了第二个按钮,它将在 3 和其他按钮之间进行切换。但我希望代码的开头在两种情况下都相同。我尝试了一些东西,但它并没有按照我想要的方式工作。谢谢您的帮助!
更新:我修复了错误。所以现在我有这个:
- (void)viewDidLoad
{
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"] ]) {
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *alerta = [[UIAlertView alloc]initWithTitle:@"Welcome!" message:@"Select your language" delegate:self cancelButtonTitle:@"Button1" otherButtonTitles:@"Button2", nil];
[alerta show];
[alerta release];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
progressView.progress += 0.25;
scan.hidden = YES;
if (progressView.progress == 1 ) {
label.hidden = YES;
progressView.hidden = YES;
[timer invalidate];
imagesText.hidden = NO;
int randomNumber;
if ([buttonTitle isEqualToString:@"Button1"]) {
randomNumber = arc4random() % 3;
}else if ([buttonTitle isEqualToString:@"Button2"]) {
randomNumber = arc4random() % 3 + 3;
}
switch (randomNumber) {
//CODE
}
}
}
问题是所有代码都应该从以下位置调用:
- (IBAction)scan:(id)sender {
label.hidden = NO;
imagesText.hidden = YES;
progressView.hidden = NO;
progressView.progress = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(alertView) userInfo:nil repeats:YES];
}
所以我将@selector 更改为alertView 之一,现在当我按下执行所有代码的按钮时,它给了我SIGABRT 错误。任何想法为什么?谢谢你的帮助!