自定义 UIButton 从表视图中钻取?
我已经放入了一个自定义的“后退”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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 IB 中执行此操作,也可以在代码中调用
addTarget:action:forControlEvents:
forUIControlEventTouchUpInside
在按钮按下时调用方法(操作)。被点击。例如:这记录在 UIControl 类参考中,UIButton是UIControl的子类。
您需要一个在事件发生时调用的方法,我在上面的代码中将其称为
backButtonPressed:
。该方法采用一个参数,即发送事件的对象,如下所示:You can do this in IB or you can do it in code with a call to
addTarget:action:forControlEvents:
forUIControlEventTouchUpInside
to invoke a method (action) when the button is tapped. For example: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: