How to fire uibarbuttonitem click event programmatically

后端 未结 8 2293
旧时难觅i
旧时难觅i 2020-12-05 05:41

I have created a UIActionSheet

UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@\"\"
                                                       


        
相关标签:
8条回答
  • 2020-12-05 05:47

    You can use this method to fire the tap event programmatically for a specific button:

    [self performSelector:@selector(buttonClicked:) withObject:self.myButton afterDelay:0.0]; 
    
    0 讨论(0)
  • 2020-12-05 05:47

    Well this is how I use actionSheet ..

    actionSheet  = [[UIActionSheet alloc] initWithTitle:nil 
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];
    
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    
    [actionSheet addSubview:pickerView];
    [pickerView release];
    
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];
    [closeButton release];
    
    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    
    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    

    Once you are done with this just define a selector in your .m like..

    -(void)dismissActionSheet{
        [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
    }
    

    so inside dismiss action sheet you can re-write what is happening inside bar button item... hope this helps.

    0 讨论(0)
  • 2020-12-05 05:53

    Another way to do that and avoiding warnings is as follows:

    [[UIApplication sharedApplication] sendAction:barButtonItem.action
                                               to:barButtonItem.target
                                             from:nil
                                         forEvent:nil];
    
    0 讨论(0)
  • 2020-12-05 05:54

    I've read the accepted answer and it is awfully DANGEROUS. you should never suppress such warnings in order to shortcut the path to your desired result!

    the safest way to do so:

    SEL selector=barButton.action;
    id target=barButton.target;
      if(selector && target){
         IMP imp = [target methodForSelector:selector];
         void (*func)(id, SEL) = (void *)imp;
         func(target, selector);
      }
    

    Please read the original post here: performSelector may cause a leak because its selector is unknown

    0 讨论(0)
  • 2020-12-05 05:59

    Implement the delegate method

    - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
    

    OR

    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex == actionSheet.destructiveButtonIndex)
        {
    
        }
        else if(buttonIndex == (actionSheet.cancelButtonIndex))
        {
            // Call your event here
            // Fire the event of UIBarButtonItem.
        }
    
    }
    

    actionSheet:didDismissWithButtonIndex:

    Sent to the delegate after an action sheet is dismissed from the screen.

    - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex Parameters

    actionSheet The action sheet that was dismissed. buttonIndex The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

    Discussion: This method is invoked after the animation ends and the view is hidden.

    actionSheet:willDismissWithButtonIndex:

    Sent to the delegate before an action sheet is dismissed.

    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex Parameters

    actionSheet The action sheet that is about to be dismissed. buttonIndex The index of the button that was clicked. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

    Discussion This method is invoked before the animation begins and the view is hidden.

    0 讨论(0)
  • 2020-12-05 06:07

    @ton1n8o 's solution worked for me. Here is the implementation in swift:

    UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: nil, forEvent: nil)
    
    0 讨论(0)
提交回复
热议问题