【问题标题】:Change the image of the correct button with an UIImagePickerController according to which button was pressed根据按下的按钮,使用 UIImagePickerController 更改正确按钮的图像
【发布时间】:2023-03-29 02:01:02
【问题描述】:

这是问题: 我希望用户点击一个按钮并选择该按钮将代表什么图像。有多个按钮,用户可以选择为他/她点击的每个按钮选择不同的图像或相同的图像。 如何在 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];


}

提前致谢,是的,我对这种语言很陌生。

【问题讨论】:

    标签: iphone ios uibutton


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      @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];
      }
      

      【讨论】:

      • 感谢您的努力,但我更喜欢这个答案:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      相关资源
      最近更新 更多