[英]iOS - Set the code to 4 UIImageViews
在我的應用中,有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
首先,您的代碼極其冗余。 它是完全相同的4倍,只是標記的值不同。 使用變量 ,只編寫一次代碼!
其次,您引用的代碼與問題無關。 假設您還為圖像視圖提供了與按鈕/警報相同的標簽,則需要在圖像選擇器控制器callback中將圖像分配給正確的圖像視圖。 這是合乎邏輯的,因為在分配圖像之前必須知道選擇了哪個圖像。 采用
imagePickerController:didFinishPickingMediaWithInfo:
您可以將變量保留為int lastButtonTag
以便知道最后按下哪個按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.