UIBarButtonItem 不可点击

发布于 2024-12-09 04:33:26 字数 1097 浏览 4 评论 0原文

我尝试在不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-12-16 04:33:26

您的视图控制器的视图是一个UIImageViewUIImageView的用户交互默认是禁用的。因此它也禁用了所有子视图的用户交互(这就是按钮不响应触摸的原因)。你必须明确设置,

backView.userInteractionEnabled = YES;

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,

backView.userInteractionEnabled = YES;
时光病人 2024-12-16 04:33:26

您能提供 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.

月野兔 2024-12-16 04:33:26

马克西姆·卡佩尔,

试试这个代码,

UIImage* image = [UIImage imageNamed:@"back.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton* backgbtn = [[UIButton alloc] initWithFrame:frame];
    [backgbtn setBackgroundImage:image forState:UIControlStateNormal];
    [backgbtn setShowsTouchWhenHighlighted:YES];
    [backgbtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgbtn];
    [self.navigationItem setLeftBarButtonItem:backBarButtonItem];
    [backBarButtonItem release];
    [backgbtn release];

Maxime Capelle,

Try this code,

UIImage* image = [UIImage imageNamed:@"back.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton* backgbtn = [[UIButton alloc] initWithFrame:frame];
    [backgbtn setBackgroundImage:image forState:UIControlStateNormal];
    [backgbtn setShowsTouchWhenHighlighted:YES];
    [backgbtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backgbtn];
    [self.navigationItem setLeftBarButtonItem:backBarButtonItem];
    [backBarButtonItem release];
    [backgbtn release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文