UIBUTTON,放"target"和"action"



我有以下代码在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];

我会在按下按钮时调用一个方法,我补充说,以下行,但不起作用:

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

你应该为 UIButton 提供正确的帧属性,现在它有 CGRectZero:

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

可:框架和边界有什么区别?

我想到的第一件事是你正在为 UIButton 设置操作,但你正在使用 UIButton 的视图初始化 UIBarButtonItem。操作不会保存在视图中。您尝试设置 UIBarButtonItem 的操作。

相关内容

最新更新