[英]Change the image of the correct button with an UIImagePickerController according to which button was pressed
This is the issue: I want the user to tap a button and chose what image the button will represent. 这就是问题所在:我希望用户点击一个按钮并选择该按钮将代表的图像。 There is more than one button and the user can choose to pick a different image or the same one for each button he/she taps. 有一个以上的按钮,用户可以选择为他/她轻按的每个按钮选择不同的图像或相同的图像。 How can I add an if structure in the void method to check which button has been pressed? 如何在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];
}
Thanks in advance, and yes I am quite new to this language. 在此先感谢您,是的,我对这种语言还很陌生。
Just keep the tag of the button pressed. 只需保持按下按钮的标签即可。 For example; 例如;
@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];
}
In -(IBAction)Buttonclicked:(id)sender
, the button clicked is sender
. 在-(IBAction)Buttonclicked:(id)sender
,单击的按钮是 sender
。 So now you know what button was clicked. 现在,您知道单击了什么按钮。
So now the only question is how to refer to sender
from within a different method. 因此,现在唯一的问题是如何从不同的方法中引用sender
。
This is why there are instance variables . 这就是为什么有实例变量的原因 。 You must prepare a UIButton instance variable or property. 您必须准备一个UIButton实例变量或属性。 Let's call it theButton
. 我们称之为theButton
。 Then in Buttonclicked:
you will set theButton
(to sender
). 然后在Buttonclicked:
您将设置 theButton
(给sender
)。 In any other method, you can then get theButton
and do anything you like to it. 然后,可以使用其他任何方法获取 theButton
并对其执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.