UIBarButtonItem: target-action not working?

后端 未结 11 896
无人及你
无人及你 2020-12-01 08:47

I\'ve got a custom view inside of a UIBarButtonItem, set by calling -initWithCustomView. My bar button item renders fine, but when I tap it, it doe

相关标签:
11条回答
  • 2020-12-01 09:28

    I had the same problem, but was averse to using a UIButton instead of a custom view for my UIBarButtonItem (per drawnonward's response).

    Alternatively, you could add a UIGestureRecognizer to the custom view before using it to initialize UIBarButtonItem; this appears to work in my project.

    This is how I would modify your original code:

    UIImageView *SOCImageView = [[UIImageView alloc] initWithImage:
                                 [UIImage imageNamed:@"cancel_wide.png"]];
    
    UITapGestureRecognizer *tapGesture = 
           [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                   action:@selector(deselectAll:)];
    [SOCImageView addGestureRecognizer:tapGesture];
    
    SOItem.leftBarButtonItem = 
           [[[UIBarButtonItem alloc] initWithCustomView:SOCImageView] autorelease];
    [tapGesture release];
    [SOCImageView release];
    
    0 讨论(0)
  • 2020-12-01 09:34

    Here's how I implemented the UIButton inside of the UIBarButtonItem:

    UIButton *logoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoButton setImage: [UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    logoButton.frame = CGRectMake(0, 0, 30, 30);
    [logoButton addTarget:self action:@selector(showAbout:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:logoButton];
    self.navigationItem.rightBarButtonItem = barItem;
    [barItem release];
    
    0 讨论(0)
  • 2020-12-01 09:35

    Here is how I make it work:

    UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(displayAboutUs) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem* itemAboutUs =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
    …
    
    0 讨论(0)
  • 2020-12-01 09:37

    Swift 3 : Below is my full implementation for button customization and event handling.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let button = UIButton.init(type: .custom)
        button.setTitle("Tester", for: .normal)
        button.setTitleColor(.darkGray, for: .normal)
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 5
        button.layer.borderColor = UIColor.darkGray.cgColor
        button.addTarget(self, action: #selector(self.handleButton), for: .touchUpInside)
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if let button = self.navigationItem.rightBarButtonItem?.customView {
            button.frame = CGRect(x:0, y:0, width:80, height:34)
        }
    }
    
    func handleButton( sender : UIButton ) {
        // It would be nice is isEnabled worked...
        sender.alpha = sender.alpha == 1.0 ? 0.5 : 1.0
    }
    

    Hope this helps

    0 讨论(0)
  • 2020-12-01 09:38

    Swift 3. I had a similar issue where I had a custom view inside of the bar button item. In order to get the tap to work I assigned a gesture to the view and set the action. The view needed to be an outlet to assign to it. Doing this it worked with the custom view.

    Setting the gesture as class variable

    @IBOutlet weak var incidentView: UIView!
    let incidentTap = UITapGestureRecognizer()
    

    In viewDidLoad

    incidentTap.addTarget(self, action: #selector(self.changeIncidentBarItem))
    incidentView.addGestureRecognizer(incidentTap)
    
    0 讨论(0)
提交回复
热议问题