Reputation: 2445
I am trying to create a simple button, textfield, label program in objective c and I am having button problems.
In file viewController.m
, I have the following code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Look up" forState:UIControlStateNormal];
button.frame = CGRectMake(20,108,97,37);
[self.view addSubview:button];
and
- (IBAction) buttonPressed:(id) sender {
[_textField resignFirstResponder];
_label.text = textField.text;
}
From what I understand, the button is supposed to to take what is in my text box and put it into a label after I click this button. But instead, pressing the button takes me main.m
on the return line. And it says the program is paused. If I try to use the program again, it just doesn't work. I have cross examined this button snippet with other code online and can't quite figure out why mine is failing.
Upvotes: 0
Views: 105
Reputation: 31745
You have missed a colon on this line
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchDown];
Your button is calling a method that doesn't exist, thats why it is crashing.
should be
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
OR... change the signature of your buttonPressed method from
- (IBAction) buttonPressed:(id) sender;
to
- (IBAction) buttonPressed;
you should have seen a message in your log, something like
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController buttonPressed]: unrecognized selector sent to instance
Upvotes: 1