Reputation: 1173
Is there any reason the following code for setting the target of a UIButton to an IBAction programmatically shouldn't work?
.h
- (IBAction)googleIt:(id)sender;
.m
UIButton *google = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 15.0, 280.0, 40.0)];
[google addTarget:self action:@selector(googleIt:) forControlEvents:UIControlEventTouchUpInside];
(that's in view did load, this is after that)
-(IBAction)googleIt:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
}
Edit: added a : after googleIt, still doesn't work. When I add a button in storyboard and just connect it to the IBAction, that works, so the IBAction itself is not the problem.
Edit: full button code requested
UIButton *google = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 15.0, 280.0, 40.0)];
[google addTarget:self action:@selector(googleIt:) forControlEvents:UIControlEventTouchUpInside];
[google setTitle:@"Google 'em" forState:UIControlStateNormal];
[google setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
google.backgroundColor = [UIColor blackColor];
[charityInfo addSubview:google];
EDIT: DONE! User action not enabled. Can somebody put that answer and I'll markas true?
Upvotes: 0
Views: 3320
Reputation: 1549
the selector for googleIt is missing the colon to indicate that it takes an argument
it should be
[google addTarget:self action:@selector(googleIt:) forControlEvents:UIControlEventTouchUpInside];
Edit, added from comments: the selector was part of the problem, but user interaction was not enabled on the view the button was contained in.
Upvotes: 2