我有以下代码在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提供正确的frame属性,因为它具有CGRectZero:

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

Cocoa: What's the difference between the frame and the bounds?

关于objective-c - uibutton,放置“目标”和“ Action ”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10775208/

10-13 04:28