我有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