UIBarButtonItem单击侧面菜单未打开

时间:2017-06-05 05:58:43

标签: ios objective-c uibarbuttonitem

我正在自定义UIBarButtonItem上开发iOS App.Add UIView。按钮已成功添加。但是,当我点击按钮时输入选择器操作。但是侧面菜单未打开。 请提前帮助谢谢。

以下是代码:

- (void)sideButton{
    UIImage *menuButtonImage = [UIImage imageNamed:@"menu-icon.png"];// set your image Name here
    UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnToggle setImage:menuButtonImage forState:UIControlStateNormal];
    [btnToggle addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    btnToggle.frame = CGRectMake(10, 30, 25, 25);
    [_sideView addSubview:btnToggle]; //sideView is UIView

    _menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnToggle];
    self.navigationItem.leftBarButtonItem = _menuBarButton;
}
- (IBAction)checkButtonTapped:(id)sender{
    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController ){
        [_menuBarButton setTarget: self.revealViewController];
        [_menuBarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }
}

2 个答案:

答案 0 :(得分:0)

您需要为checkButtonTapped添加方法。

-(void) checkButtonTapped:(UIButton*)sender {
//write your button action here.
}

如果您想使用现有方法,则必须在 addTarget 方法中提及。

答案 1 :(得分:0)

尝试更改此

UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];

进入这个

UIButton *btnToggle = [[UIButton alloc]initWithFrame:CGRectMake(0,0,50,50)];

告诉我这是否有效