1

这就是问题所在:我希望用户点击一个按钮并选择该按钮将代表什么图像。有多个按钮,用户可以选择为他/她点击的每个按钮选择不同的图像或相同的图像。如何在 void 方法中添加 if 结构来检查按下了哪个按钮?

@implementation ViewController
@synthesize tegelEen,tegelTwee;  //tegelEen is a button an so is tegelTwee

-(IBAction)Buttonclicked:(id)sender {
    picController = [[UIImagePickerController alloc]init];
    picController.delegate = self;
    picController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picController animated:YES completion:nil];

}



-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *btnImage = [info objectForKey:UIImagePickerControllerOriginalImage];

        //Now it changes both buttons but I want it to change only the one that was clicked.
        [tegelEen setImage:btnImage forState:UIControlStateNormal]; 
        [tegelTwee setImage:btnImage forState:UIControlStateNormal];

    [self dismissViewControllerAnimated:YES completion:nil];


}

在此先感谢,是的,我对这种语言很陌生。

4

2 回答 2

0

-(IBAction)Buttonclicked:(id)sender中,单击的按钮 sender。所以现在你知道点击了哪个按钮。

所以现在唯一的问题是如何sender从不同的方法中引用。

这就是为什么会有实例变量。您必须准备一个 UIButton 实例变量或属性。让我们称之为theButton。然后在Buttonclicked:你将设置 theButton(到sender)。在任何其他方法中,您都可以获取 theButton并做任何您喜欢的事情。

于 2013-05-07T22:55:20.347 回答
0

只需按住按钮的标签即可。例如;

@implementation ViewController
@synthesize tegelEen,tegelTwee;  //tegelEen is a button an so is tegelTwee
int lastClickedButtonTag;

- (void)viewDidLoad
{
    tegelEen.tag = 1;
    tegelTwee.tag = 2;
}

-(IBAction)Buttonclicked:(UIButton *)sender 
{
    lastClickedButtonTag = sender.tag;

    picController = [[UIImagePickerController alloc]init];
    picController.delegate = self;
    picController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picController animated:YES completion:nil];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ 
    UIImage *btnImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    //Now it changes the button that was clicked.

    if (lastClickedButtonTag == 1) [tegelEen setImage:btnImage forState:UIControlStateNormal]; 
    else if (lastClickedButtonTag == 2) [tegelTwee setImage:btnImage forState:UIControlStateNormal];

    [self dismissViewControllerAnimated:YES completion:nil];
}
于 2013-05-07T22:25:42.090 回答