我有UINavigationBar
与使用自定义视图创建的按钮。我为每个按钮添加了一个选择器,但按钮没有响应选择器。你可以帮我吗?按钮不起作用
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setTitle:@"Title"];
// create custom view
container = [[UIView alloc] init];
[container setUserInteractionEnabled:TRUE];
NSString *path;
// create 2 buttons and put in a custom view
buttonList = [[UIButton alloc] init];
path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"];
UIImage *imgList = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"];
UIImage *imgListPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[buttonList setImage:imgListPrs forState:UIControlStateSelected];
[buttonList setSelected:TRUE];
[buttonList setFrame:CGRectMake(-30, 0, imgList.size.width, imgList.size.height)];
[buttonList setImage:imgList forState:UIControlStateNormal];
[container addSubview:buttonList];
[buttonList addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonList setUserInteractionEnabled:TRUE];
buttonPic = [[UIButton alloc] init];
path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"];
UIImage *imgPic = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"];
UIImage *imgPicPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[buttonPic setImage:imgPicPrs forState:UIControlStateSelected];
[buttonPic setFrame:CGRectMake(-10, 0, imgPic.size.width, imgPic.size.height)];
[buttonPic setImage:imgPic forState:UIControlStateNormal];
[container addSubview:buttonPic];
[buttonPic addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonPic setUserInteractionEnabled:TRUE];
// create UIBarButtonItem with a custom view
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
// put item on navigation bar
[self.navigationItem setRightBarButtonItem:item];
}
[[UIButton alloc] init]是不好的样式 - 使用工厂方法[UIButton buttonWithStyle :. ..]代替。 – Eiko
那么最好在这种情况下使用:'[[UIButton alloc] initWithCustomView:];' – Nekto
如何创建按钮并没有找到方法buttonWithStyle:和initWithCustomView :.有buttonWithType:和init :, initWithFrame:和initWithCoder :. – Sveta