1

希望有人能帮助我解释为什么我应该添加一个 : 来调用该方法。

这是 viewDidLoad 方法

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor= [UIColor yellowColor];
//Add Button
CGRect buttonRect= CGRectMake(100, 100, 100, 44);
UIButton *clickButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[clickButton setFrame: buttonRect];
[clickButton setTitle:@"Click Me" forState: UIControlStateNormal];
[clickButton addTarget:self action:@selector(buttonPressed:)    forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickButton];
[self buttonPressed:clickButton];
//Add UILabel
CGRect labelViewRect= CGRectMake(50, 30, 200, 44);
UILabel *labelview= [[UILabel alloc]initWithFrame:labelViewRect];
[labelview setText:@"Hello welcome to my app!"];
//Clear the UIView button's background
[labelview setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:labelview];
}

这是按钮按下方法

-(void)buttonPressed:(UIButton *)sender{
NSLog(@"This button was pressed");
self.view.alpha=((double)arc4random()/0x100000000);
}

当我删除 : 从

    [clickButton addTarget:self action:@selector(buttonPressed:)    forControlEvents:UIControlEventTouchUpInside];

即正在将上面的行更改为类似的内容

 [clickButton addTarget:self action:@selector(buttonPressed)    forControlEvents:UIControlEventTouchUpInside];

抛出异常:

2012-12-29 17:56:18.209 AlphaSchoolColor[11414:c07]-[com_skminfotekViewController buttonPressed]:无法识别的选择器发送到实例 0xde2f050 2012-12-29 17:56:18.210 AlphaSchoolColor[11414:c07] *由于未捕获而终止应用程序异常“NSInvalidArgumentException”,原因:“-[com_skminfotekViewController buttonPressed]:无法识别的选择器发送到实例 0xde2f050”

向社区提出的问题是它正在发生什么:我可以在哪里了解更多关于它的重要性的信息。

谢谢。

4

1 回答 1

3

你的方法有一个参数。如果您想发送不带“:”的操作,请更改您的方法。

-(void)buttonPressed{
}
于 2012-12-30T00:06:57.490 回答