[英]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.