加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UIBarButtonItem init,自定义视图选择器无法正常工作

发布时间:2020-12-14 18:44:57 所属栏目:百科 来源:网络整理
导读:我正在努力让左按钮正常工作并模仿后退按钮. 我创建按钮的代码: UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];backButton.target =
我正在努力让左按钮正常工作并模仿后退按钮.

我创建按钮的代码:

UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];

backButton.target = self;
backButton.action = @selector(buttonPressed:);

self.navigationItem.leftBarButtonItem = backButton;

这里调用创建自定义按钮的方法:

- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {

UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];

CGRect frameCustomButton = CGRectMake(x,y,customImageDefault.size.width/widthDivider,customImageDefault.size.height/heightDivider);

UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];

[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];

UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

return barCustomButton;

}

行动:

-(void)buttonPressed:(id) sender{

    NSLog(@"Entered");
    SearchViewController *ViewController = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:ViewController animated:YES];

}

所以我能用一个简单的UIButton但不能用UIButtonBarItem来制作它,我真的不知道它是怎么回事.

如果你能帮助我,我会非常感激.

谢谢.

解决方法

这样做会将选择器添加到自定义按钮,因为它是bar buttom的视图:

[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

编辑:注意:UIBarButtonItem的目标和操作适用于自定义视图.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读