Im trying to do an app with 4 ImageViews and 4 UIButtons. When one of the UIButtons are pressed a UIAlertView should appear with 3 options. One named “Picture” should open the photo library so the user can change the ImageView picture. I have wrote a code for this but it wont work. Do anyone have any suggestions on how to make it work?
Thanks in advance!
- (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
You should use the
tagproperty of UIAlertView to differentiate between the alerts and thebuttonIndex(not the button title) to know which button was pressed.So don’t use isEqualToString at all in the alert view delegate method. You want this to work with different languages.
In the button action, before you show the alert:
Alert view delegate: