button不工作的iOS

我在IB中插入一个button,将这个代码添加到.h文件中

-(IBAction)buttonPressed:(id)sender; 

将操作连接到IB(Touch Up Inside)中的button,然后在@synthesize下面的.m文件中

 -(IBAction)buttonPressed:(id)sender { NSLog(@"Button pressed"); ... } 

没有任何东西显示在控制台中,所以出于某种原因没有执行该操作。 为什么是这样? 谢谢。

这个方法对我来说显得很好。 仔细检查:

  1. 您已经在IB中设置了包含button的视图控制器的类到正确的类。

  2. 当你右键点击IB中的button时, IBAction已经连接上了。

你没有显示你的代码的其余部分 – 它应该看起来像

 YourViewController *yourViewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil]; yourViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:yourViewController animated:YES]; 

请确保您已将YourViewController.h包含在调用View Controller的.m文件中。