【问题标题】:Assign IBAction to custom navigationBarItem [duplicate]将 IBAction 分配给自定义 navigationBarItem [重复]
【发布时间】:2013-06-27 03:45:26
【问题描述】:

我使用自定义 UIViews 作为 navigationBarItem(就像 Facebook 在他们的 iOS 应用程序中那样)。

UIImageView *linkesMenu = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"187menu.png"]];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:linkesMenu];

barItem.action = @selector(MenuSlider:);
self.navigationItem.leftBarButtonItem = barItem;

我尝试了这个和其他一些东西,但没有任何效果。

【问题讨论】:

    标签: ios objective-c uinavigationbar custom-view


    【解决方案1】:

    先尝试将图片放入按钮中:

    UIImage *theImage = [UIImage imageNamed:@"image.png"];
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    theButton.bounds = CGRectMake(0, 0, theImage.size.width, theImage.size.height);
    [theButton setImage:theImage forState:UIControlStateNormal];
    UIBarButtonItem *theBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:theButton];
    
    
    [theButton addTarget:self action:@selector(MenuSlider:) forControlEvent:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = barItem;
    

    【讨论】:

    • 然后像上面那样添加动作?
    • 是的,应该可以。
    • 好的,除了它不做任何事情之外它确实可以正常工作:/我可以选择按钮,但它不会执行我的 IBAction....
    • 感谢您更新答案。很高兴看到你成功了!
    • 感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多