0

私のアプリには4つのUIButtonがあり、これらはすべて4つのUIAlerViewに接続されているため、ボタンを押すとUIAlertViewがポップアップ表示されます。これは、すべてのUIButtonで正常に機能します。UIAlertViewのオプションの1つは、ユーザーが各UIButtonに接続されている4つのUIImageViewの画像を変更できるように、フォトライブラリを開く必要があります。問題は、UIButtonから画像を選択すると、その写真が1つのImageView、つまり4番目の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

1 に答える 1

1

まず、コードは非常に冗長です。タグの値を除いて、まったく同じ4倍です。変数を使用して、コードを1回だけ記述してください。

第二に、あなたが引用するコードは問題とは何の関係もありません。画像ビューにもボタン/アラートと同じタグを付けたとすると、画像ピッカーコントローラーのコールバックで画像を正しい画像ビューに割り当てる必要があります。画像を割り当てる前に、どの画像が選択されているかを知る必要があるため、これは論理的です。使用する

imagePickerController:didFinishPickingMediaWithInfo:

int lastButtonTagどのボタンが最後に押されたかがわかるように、変数を保持することができます。

于 2012-11-24T11:06:24.437 に答える