-2

4つのImageViewと4つのUIButtonを使用してアプリを実行しようとしています。UIButtonの1つを押すと、UIAlertViewが3つのオプションとともに表示されます。「Picture」という名前のユーザーがフォトライブラリを開いて、ユーザーが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
4

1 に答える 1

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
        }
    }
}
于 2012-11-20T21:26:55.837 に答える