UIBarButtonItem 不可点击
我尝试在不使用 xib 文件的情况下创建视图。 不幸的是,我在开发初期遇到了问题。
我只想在其中添加一个导航栏和一个确定按钮。 一切似乎都很好,但在模拟器中,当我单击按钮时,没有任何反应。 选择器未被调用,并且似乎也未到达按钮(外观没有变化)。
如果您可以查看我的代码,将会有所帮助。
-(void)loadView {
//Set view background
UIImageView * backView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:MM_BACKGROUND]] autorelease];
[self setView:backView];
//Set navigationBar
UINavigationBar* navBar = [[[UINavigationBar alloc] initWithFrame:CGRectZero] autorelease];
navBar.frame = CGRectMake(0, 0, 320, 44);
navBar.tintColor = [UIColor clearColor];
navBar.barStyle = UIBarStyleBlackTranslucent;
UINavigationItem* navBarTitle = [[[UINavigationItem alloc] initWithTitle:@"My title"] autorelease];
UIBarButtonItem* backButton = [[[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(onTouchBackButton)] autorelease];
navBarTitle.leftBarButtonItem = backButton;
[navBar pushNavigationItem:navBarTitle animated:NO];
[self.view addSubview:navBar];
}
谢谢马克西姆
I try to create a view without using xib file.
Unfortunately, I have problems early in my development.
I just want to add a navigationBar and an OK button in it.
Everything seems to be ok but in the simulator when I click on the button, nothing happens.
The selector is not called and it seems also that the button is not reached (no change of the aspect).
If you can have a look to my code, it would help.
-(void)loadView {
//Set view background
UIImageView * backView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:MM_BACKGROUND]] autorelease];
[self setView:backView];
//Set navigationBar
UINavigationBar* navBar = [[[UINavigationBar alloc] initWithFrame:CGRectZero] autorelease];
navBar.frame = CGRectMake(0, 0, 320, 44);
navBar.tintColor = [UIColor clearColor];
navBar.barStyle = UIBarStyleBlackTranslucent;
UINavigationItem* navBarTitle = [[[UINavigationItem alloc] initWithTitle:@"My title"] autorelease];
UIBarButtonItem* backButton = [[[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(onTouchBackButton)] autorelease];
navBarTitle.leftBarButtonItem = backButton;
[navBar pushNavigationItem:navBarTitle animated:NO];
[self.view addSubview:navBar];
}
Thanks
Maxime
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的视图控制器的视图是一个UIImageView。 UIImageView的用户交互默认是禁用的。因此它也禁用了所有子视图的用户交互(这就是按钮不响应触摸的原因)。你必须明确设置,
Your view controller's view is an UIImageView. UIImageView's user interaction is disabled by default. So it disables the user interaction of all its subviews too (that's why the button doesn't respond to touches). You have to explicitly set,
您能提供
onTouchBackButton
选择器的代码吗?但只是一个疯狂的猜测,也许你应该将代码更改为
...action:@selector(onTouchBackButton:)] autorelease];
注意 : 的存在,这是需要的因为您的
IBAction
需要一个 arg(id) sender
。Can you provide the code for
onTouchBackButton
selector ?But just a wild guess, maybe you should change your code to
...action:@selector(onTouchBackButton:)] autorelease];
notice the presence of : this is needed as your
IBAction
takes an arg(id) sender
.马克西姆·卡佩尔,
试试这个代码,
Maxime Capelle,
Try this code,