Passing parameters on button action:@selector

前端 未结 11 1311
生来不讨喜
生来不讨喜 2020-11-27 17:29

I want to pass the movie url from my dynamically generated button to MediaPlayer:

[button addTarget:self action:@selector(buttonPressed:) withObject:[speaker         


        
相关标签:
11条回答
  • 2020-11-27 18:01

    You can sub-class a UIButton named MyButton, and pass the parameter by MyButton's properties.

    Then, get the parameter back from (id)sender.

    0 讨论(0)
  • 2020-11-27 18:04

    You can set tag of the button and access it from sender in action

    [btnHome addTarget:self action:@selector(btnMenuClicked:)     forControlEvents:UIControlEventTouchUpInside];
                        btnHome.userInteractionEnabled = YES;
                        btnHome.tag = 123;
    

    In the called function

    -(void)btnMenuClicked:(id)sender
    {
    [sender tag];
    
        if ([sender tag] == 123) {
            // Do Anything
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:04

    I have another solution in some cases.

    store your parameter in a hidden UILabel. then add this UILabel as subview of UIButton.

    when button is clicked, we can have a check on UIButton's all subviews. normally only 2 UILabel in it.

    one is UIButton's title, the other is the one you just added. read that UILabel's text property, you will get the parameter.

    This only apply for text parameter.

    0 讨论(0)
  • 2020-11-27 18:12

    Edit. Found a neater way!

    One argument that the button can receive is (id)sender. This means you can create a new button, inheriting from UIButton, that allows you to store the other intended arguments. Hopefully these two snippets illustrate what to do.

      myOwnbutton.argOne = someValue
      [myOwnbutton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    

    and

    - (IBAction) buttonTouchUpInside:(id)sender {
      MyOwnButton *buttonClicked = (MyOwnButton *)sender; 
      //do as you please with buttonClicked.argOne
    }
    

    This was my original suggestion.

    There is a way to achieve the same result, but it's not pretty. Suppose you want to add a parameter to your navigate method. The code below will not allow you to pass that parameter to navigate.

    [button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
    

    To get around this you can move the navigate method to a class of it's own and set the "parameters" as attributes of that class...

    NavigationAid *navAid = [[NavigationAid alloc] init];
    navAid.firstParam = someVariableOne
    navAid.secondParam = someVariableTwo
    [button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
    

    Of course you can keep the navigate method in the original class and have it called by navAid, as long as it knows where to find it.

    NavigationAid *navAid = [[NavigationAid alloc] init];
    navAid.whereToCallNavigate = self
    navAid.firstParam = someVariableOne
    navAid.secondParam = someVariableTwo
    [button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
    

    Like I said, it's not pretty, but it worked for me and I haven't found anyone suggesting any other working solution.

    0 讨论(0)
  • 2020-11-27 18:13

    Add the hidden titleLabel to the parameter is the best solution.

    I generated a array arrUrl which store the NSURL of the mov files in my phone album by enumerate assets block.

    After that, I grab on Frame, lets say, get the frame at 3:00 second in the movie file, and generated the image file from the frame.

    Next, loop over the arrUrl, and use program generate the button with image in the button, append the button to subview of the self.view.

    Because I have to pass the movie Url to playMovie function, I have to assign the button.titleLabel.text with one movie url. and the the button events function, retrieve the url from the buttontitleLable.txt.

    -(void)listVideos
    
    {
       for(index=0;index<[self.arrUrl count];index++{
          UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
          imageButton.frame = CGRectMake(20,50+60*index,50,50);
    
          NSURL *dUrl = [self.arrUrl objectAtIndex:index];
    
          [imageButton setImage:[[UIImage allow] initWithCGImage:*[self getFrameFromeVideo:dUrl]] forState:UIControlStateNormal];
          [imageButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
          imageButton.titleLabel.text = [NSString strinfWithFormat:@"%@",dUrl];
          imageButton.titleLabel.hidden = YES;
    
          [self.view addSubView:imageButton];
       }
    }
    
    -(void)playMovie:(id) sender{
       UIButton *btn = (UIButton *)sender;
       NSURL *movUrl = [NSURL URLWithString:btn.titleLabel.text];
       moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movUrl];
       [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    }
    
    -(CGIImageRef *)getFrameFromVideo:(NSURL *)mUrl{
       AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:mUrl option:nil];
       AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
       generator.appliesPreferredTrackTransform = YES;
       NSError *error =nil;
       CMTime = CMTimeMake(3,1);
       CGImageRef imageRef = [generator copyCGImageAtTime:time actualTime:nil error:&error];
       if(error !=nil) {
          NSLog(@"%@",sekf,error);
       }
    
       return @imageRef;
    }
    
    0 讨论(0)
提交回复
热议问题