简体   繁体   English

uibutton,把“目标”和“行动”

[英]uibutton, put “target” and “action”

I have the following code that adds a button of type UIBarButtonItem on UIToolbar (postoInfo): 我有以下代码在UIToolbar(postoInfo)上添加UIBarButtonItem类型的按钮:

UIImage *faceImage = [UIImage imageNamed:@"informazioni.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];

[face addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];

face.bounds = CGRectMake( 0, 0, 30, 30 );
[face setImage:faceImage forState:UIControlStateNormal];

buttonOne = [[UIBarButtonItem alloc] initWithCustomView:face];

NSArray *buttons = [NSArray arrayWithObjects: buttonOne, nil];

[postoInfo setItems: buttons animated:YES];

I would call a method when the button is pressed, I added, the following line, but does not work: 按下按钮时我会调用一个方法,我添加了以下行,但不起作用:

[face addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];

You should provide correct frame property for UIButton, now it has CGRectZero: 你应该为UIButton提供正确的框架属性,现在它有CGRectZero:

face.frame = CGRectMake( 0, 0, 30, 30 ); 

Cocoa: What's the difference between the frame and the bounds? 可可:框架和边界之间有什么区别?

The first thing that comes to my mind is that you are setting the action for the UIButton, but you are initializing the UIBarButtonItem with the UIButton's view. 我想到的第一件事是你正在为UIButton设置动作,但是你正在用UIButton的视图初始化UIBarButtonItem。 The action isn't saved in the view. 该操作未保存在视图中。 You try setting the action for the UIBarButtonItem. 您尝试为UIBarButtonItem设置操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton target:action:Selector Argument - UIButton target: action: Selector Argument 不调用UIButton目标动作 - UIButton target action not being called 自定义类中的UIButton目标操作 - UIButton target action inside custom class UIButton AddTarget多次在同一个目标操作上只调用一次? - UIButton AddTarget multiple times on same target action only calls once? 具有目标操作的单元格上的iOS UIButton,单元格未在(id)sender中更新 - iOS UIButton on cell with target action, cell not updated in (id)sender UIButton addTarget:action:forControlEvents:不能将自己用作目标吗? - UIButton addTarget:action:forControlEvents: not working using itself as a target? 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? - How to add UIViewController as target of UIButton action created in programmatically created UIView? 为什么touchesBegan,target-action和touchesEnded的执行顺序会随着UIButton的快速触摸而改变? - Why does the execution order of touchesBegan, target-action and touchesEnded change with fast touches of UIButton? 按标题定位UIButton - Target a UIButton by it's title 为什么UIButton使用目标动作设计模式而不是UITextField的委派模式和反之 - Why UIButton uses Target Action Design Pattern not Delegate Pattern and Vice Versa for UITextField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM