在我的应用程序中有4个UIButton,它们都连接到4个UIAlerView,因此当按下按钮时,应弹出UIAlertView。这适用于所有UIButton。 UIAlertView中的一个选项应该打开照片库,以便用户可以更改连接到每个UIButton的4个UIImageView的图片。 问题是,当我从UIButtons中选择任何一张照片时,照片只适用于一张ImageView,第四张。 这是我的UIAlertView代码:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
//UIAlertView´s in the UIButtons setup
if (alertView.tag == 1) { //button1
if (buttonIndex == alertView.cancelButtonIndex) {
NSLog(@"Done");
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
NSLog(@"Number");
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
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];
}
}
if (alertView.tag == 2) { //button2
if (buttonIndex == alertView.cancelButtonIndex) {
NSLog(@"Done");
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
NSLog(@"Number");
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
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];
}
}
if (alertView.tag == 3)
{
if (buttonIndex == alertView.cancelButtonIndex) {
NSLog(@"Done");
}
if (buttonIndex == alertView.firstOtherButtonIndex){
NSLog(@"Number");
}
if (buttonIndex == alertView.firstOtherButtonIndex+1){
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];
}
}
if (alertView.tag == 4);
{
if (buttonIndex == alertView.cancelButtonIndex){
NSLog(@"Done");
}
if (buttonIndex == alertView.firstOtherButtonIndex){
NSLog(@"phone number");
}
if (buttonIndex == alertView.firstOtherButtonIndex+1){
imagePickerController = [[UIImagePickerController alloc]init];
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
}
@end
答案 0 :(得分:1)
首先,您的代码非常冗余。除了标签的值之外,它是完全相同的4倍。使用变量并只编写一次代码!
其次,您引用的代码与问题无关。假设您还为图像视图提供了与按钮/警报相同的标签,则需要将图像分配到图像选择器控制器回调中的正确图像视图。这是合乎逻辑的,因为您必须知道在分配图像之前拾取了哪个图像。使用
imagePickerController:didFinishPickingMediaWithInfo:
您可以保留变量int lastButtonTag
,以便了解上次按下哪个按钮。