Reputation: 13
Here's my code for ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addMyButton];
}
-(void)addMyButton {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
webView.tag=55;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 210, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];
}
- (IBAction)close:(id)sender {
// [[self.view viewWithTag:55] removeFromSuperview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[super dealloc];
}
@end
And here's ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
}
- (IBAction)close:(id)sender;
@end
All very simple, but I keep getting this error:
-[ViewController aMethod:]: unrecognized selector sent to instance 0x7141780 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController aMethod:]: unrecognized selector sent to instance 0x7141780'
Very baffling!
Upvotes: 1
Views: 539
Reputation: 5268
[button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
your code clearly shows you are adding two target . Remove the first
[button addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
and use your
- (IBAction)close:(id)sender;
Your code pasted in question will start working
Upvotes: 0
Reputation: 108169
As simple as it gets, ViewController
is not implementing -aMethod:
.
I think you missed a copy-paste and you should change the selector.
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
will cause aMethod:
on self
to fire on a touch down event on the button.
Since you haven't implemented such method, you get a crash. Not baffling at all.
Upvotes: 2
Reputation: 5732
You need to add aMethod to your ViewController class.
- (void)aMethod:(id)sender
{
}
Upvotes: 5