自定义 UIButton 从表视图中钻取?

发布于 2024-11-30 19:00:16 字数 894 浏览 1 评论 0原文

我已经放入了一个自定义的“后退”UIBarButtonItem,它来自我在 Photoshop 中制作的图像。我已经设法让它看起来很好,我的问题是让它发挥作用。

我假设我的按钮没有执行任何操作,因为我还没有为其分配任何操作。我只是不知道这样做的正确语法?

这是我的代码

    UIImage *buttonImage = [UIImage imageNamed:@"BackButton.png"];
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

// Im assuming this is where i need to put the action...
        //[backButton addTarget:self action:@selector(.......) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
                                                 initWithCustomView:backButton];

基本上,我希望我的新自定义后退按钮的功能就像常规 UITableView 中的默认后退按钮一样

, 安迪

Ive put in a custom "Back" UIBarButtonItem, which is from an Image i made in photoshop. Ive managed to make it appear just fine, my problem is making it work.

My button does nothing, im assuming because i havent assigned any actions to it. I just dont know the correct syntax to do so?

Heres my code

    UIImage *buttonImage = [UIImage imageNamed:@"BackButton.png"];
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

// Im assuming this is where i need to put the action...
        //[backButton addTarget:self action:@selector(.......) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
                                                 initWithCustomView:backButton];

Basically, i want my new custom backbutton to function just like the default one in a regular UITableView

regards,
Andyy

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

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

发布评论

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

评论(1

久光 2024-12-07 19:00:16

您可以在 IB 中执行此操作,也可以在代码中调用 addTarget:action:forControlEvents: for UIControlEventTouchUpInside 在按钮按下时调用方法(操作)。被点击。例如:

[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

这记录在 UIControl 类参考中,UIButton是UIControl的子类。

您需要一个在事件发生时调用的方法,我在上面的代码中将其称为 backButtonPressed:。该方法采用一个参数,即发送事件的对象,如下所示:

-(void)backButtonPressed:(id)sender
{
    // NOTE: assumes self is your UIViewController
    [self.navigationController popViewControllerAnimated:YES];
}

You can do this in IB or you can do it in code with a call to addTarget:action:forControlEvents: for UIControlEventTouchUpInside to invoke a method (action) when the button is tapped. For example:

[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

This is documented in UIControl class reference, UIButton is a subclass of UIControl.

You'll need a method which will be called when the event occurs, which I called backButtonPressed: in my code above. This method takes one parameter, the object sending the event, something like this:

-(void)backButtonPressed:(id)sender
{
    // NOTE: assumes self is your UIViewController
    [self.navigationController popViewControllerAnimated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文