将操作添加到自定义视图中的 UIButton

发布于 2024-10-07 17:56:42 字数 956 浏览 0 评论 0原文

我有一个自定义视图,它有一个 UIButton。我将此视图添加到我的 navigationItem 的 rightbarButtonItem 中,可以,但是我必须向此按钮添加一个操作。

这是我的代码;

-(void)showMessage:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad {
    UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
    [buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
    buttonView.view.frame = CGRectMake(0,0,95,34);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view]; 

    self.navigationItem.rightBarButtonItem = item;
    [self setTitle:@"Details"];

    [super viewDidLoad];
}

谢谢。

I have a custom view and it has a UIButton. I add this view to my navigationItem's rightbarButtonItem, it's ok, but I have to add an action to this button.

Here is my code;

-(void)showMessage:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad {
    UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:@"UpDownButtonView" bundle:nil];
    [buttonView.upButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    [buttonView.downButton addTarget:self action:@selector(prev:) forControlEvents:UIControlEventTouchUpInside];
    buttonView.view.frame = CGRectMake(0,0,95,34);
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view]; 

    self.navigationItem.rightBarButtonItem = item;
    [self setTitle:@"Details"];

    [super viewDidLoad];
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南街女流氓 2024-10-14 17:56:46

您不需要带有 UIButton 的自定义视图,您需要的是分配并初始化 UIBarButtonItem 并将其放在导航栏上,

代码如下所示:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back Button") style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];

self.navigationItem.leftBarButtonItem = backButton;
[backButton release];

You don't need a custom view with a UIButton, what you need is alloc and init a UIBarButtonItem and put it on your nav bar

the code would look like:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back Button") style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed:)];

self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文