我有以下代码在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];
答案 0 :(得分:3)
你应该为UIButton提供正确的框架属性,现在它有CGRectZero:
face.frame = CGRectMake( 0, 0, 30, 30 );
Cocoa: What's the difference between the frame and the bounds?
答案 1 :(得分:0)
我想到的第一件事是你正在为UIButton设置动作,但是你正在用UIButton的视图初始化UIBarButtonItem。该操作未保存在视图中。您尝试为UIBarButtonItem设置操作。