从UIAlertView选项xcode设置背景

时间:2012-11-20 21:02:15

标签: ios

我试图用4个ImageViews和4个UIButton做一个应用程序。当按下其中一个UIButtons时,UIAlertView应显示3个选项。一个名为“图片”的应该打开照片库,以便用户可以更改ImageView图片。我为此编写了一个代码,但它无法正常工作。有人对如何使它有任何建议吗? 提前谢谢!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *buttonOne = changeButtonOne;  //the UIButtons
NSString *buttonTwo = changeButtonTwo;
NSString *buttonThree = changeButtonThree;
NSString *buttonFour = changeButtonFour;


if([buttonOne isEqualToString:@"buttonOne"])
{
      if([title isEqualToString:@"Done"])
    {
        NSLog(@"You pressed done");
    }
      else if([title isEqualToString:@"Phone number"])
    {
        NSLog(@"You pressed Phone number");
    }
      else if([title isEqualToString:@"Picture"])
    {
        imagePickerController = [[UIImagePickerController alloc]init];   //I think its this part which are wrong
        [imagePickerController setDelegate:self];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}
else if([buttonTwo isEqualToString:@"buttonTwo"])
{
    if([title isEqualToString:@"Done"])
    {
        NSLog(@"Yoy pressed done");
    }
    else if([title isEqualToString:@"Phone number"])
    {
        NSLog(@"You pressed Phone number");
    }
    else if([title isEqualToString:@"Picture"])
    {
        imagePickerController2 = [[UIImagePickerController alloc]init];  //I think its the part thats wrong.
        [imagePickerController2 setDelegate:self];
        [imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [self presentViewController:imagePickerController2 animated:YES completion:nil];
    }

}
else if([buttonThree isEqualToString:@"buttonThree"])
    {
        if([title isEqualToString:@"Done"])
        {
            NSLog(@"You pressed done");
        }
        else if([title isEqualToString:@"Phone number"])
        {
            NSLog(@"You pressed Phone number");
        }
        else if([title isEqualToString:@"Picture"])
        {
            imagePickerController3 = [[UIImagePickerController alloc]init];   //i think this is the wrong part
            [imagePickerController3 setDelegate:self];
            [imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [self presentViewController:imagePickerController3 animated:YES completion:nil];
        }
    }

    else if([buttonFour isEqualToString:@"buttonFour"])
    {
        if([title isEqualToString:@"Done"])
        {
            NSLog(@"You pressed done");
        }
        else if([title isEqualToString:@"Phone number"])
        {
            NSLog(@"You pressed Phone number");
        }
        else if([title isEqualToString:@"Picture"])
        {
            imagePickerController4 = [[UIImagePickerController alloc]init];    //this is probably the wrong part
            [imagePickerController4 setDelegate:self];
            [imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [self presentViewController:imagePickerController4 animated:YES completion:nil];
        }
    }




}

@end

1 个答案:

答案 0 :(得分:0)

您应该使用UIAlertView的tag属性来区分警报和buttonIndex(不是按钮标题),以了解按下了哪个按钮。 因此,请勿在警报视图委托方法中使用isEqualToString。您希望这可以使用不同的语言。

在按钮操作中,在显示警报之前:

alertView.tag = 1; // 1 = button1, 2 = button2 etc.

警报视图委托:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) { //button1
        if (buttonIndex == alertView.cancelButtonIndex) {
            // handle cancel button
        }        
        if (buttonIndex == alertView.firstOtherButtonIndex) {
            // handle action 1
        }
        if (buttonIndex == alertView.firstOtherButtonIndex+1) {
            // handle action 2
        }
    }
    if (alertView.tag == 2) { //button2
        if (buttonIndex == alertView.cancelButtonIndex) {
            // handle cancel button
        }        
        if (buttonIndex == alertView.firstOtherButtonIndex) {
            // handle action 3
        }
        if (buttonIndex == alertView.firstOtherButtonIndex+1) {
            // handle action 4
        }
    }
}