button操作没有响应iphone

当我按下nextButton,这是UIBarButton。 然后关联的动作不执行。

UIButton *nextButton1=[UIButton buttonWithType:UIButtonTypeCustom]; [nextButton1 setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal]; [nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpOutside]; [nextButton1 sizeToFit]; UIBarButtonItem *nextButton=[[UIBarButtonItem alloc]initWithCustomView:nextButton1]; self.navigationItem.rightBarButtonItem=nextButton; 

 [nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside]; 

你传递的是UIControlEventTouchUpOutside ,应该是UIControlEventTouchUpInside

你通过forcontrolevent作为UIControlEventTouchUpoutside而不是使用

 [nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside]; [nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside]; 

显而易见的错误是UIControlEventTouchUpOutside ,您应该将其更改为UIControlEventTouchUpInside

但是,不要做一个单独的button,然后将其作为一个视图分配给UIBarButtonItem ,你可以简单地做一个UIBarButtonItem与所需的图像,并将其与所需的行动。

例如:

 UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"] style:UIBarButtonItemStylePlain target:self action:@selector(goToItems)]; self.navigationItem.rightBarButtonItem = nextButton; [nextButton release];