I’m a noob to iOS development (so please bear with me on this), and I’m trying to create a simple touch application with Xcode from scratch. I have spent a week on this, but I couldn’t seem to find out what I have been missing so here I am, asking for some guidance 🙂
First, I created an empty application, then created a xib file (MainWindow.xib) and added a window object (Main_Window) to it. Then, I created a View object (Main_View) within this Main_Window, and added a label object (lblTitle) to this view. The Main_View object pretty much covered the entire Main_Window screen.
So, in short, the hierarchy of my MainWindow.xib is like this: Main_Window –> Main_View –> lblTitle.
Finally, I created a ViewController object (Main_View_Controller) with its “view” set to Main_View and its “rootViewController” set to “Main_Window”.
In the project,
I subclassed UIView with “TouchEvent_View”, hooked up to “Main_View” in the xib file.
I subclassed UIViewController with “TouchEvent_ViewController”, hooked up to “Main_View_Controller” in the xib file.
In my AppDelegate.h,
I created an “IBOutlet UIWindow *window”, hooked up to “Main_Window” in the xib file.
I created an object for my viewController and view classes.
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (strong, nonatomic) TouchEvent_ViewController * myViewController;
@property (strong, nonatomic) TouchEvent_View *myView;
In AppDelegate.m, I hooked up “MainWindow.xib” with myViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.backgroundColor = [UIColor whiteColor];
self.myViewController = [[[TouchEvent_ViewController alloc]
initWithNibName:@"MainWindow" bundle:nil] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
In “Touch_Event_ViewController.m”, I coded the viewDidLoad message as followed:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Hi! ViewController's viewDidLoad msg sent!");
TouchEvent_View * mView = [[TouchEvent_View alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
mView.backgroundColor = [UIColor blueColor];
[self.view addSubview:mView];
[mView release];
}
In “TouchEvent_View.m”, I instantiated a UITapGestureRecognizer object and hooked it up to a handler method as followed:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"[TouchEvent_View initWithFrame] sent!");
// Initialization code
//-----------------------
//Touch event declaration
//Single tap
UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap_Handler)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
//singleTap.delegate = self;
[self addGestureRecognizer:singleTap];
}
return self;
}
-(void) SingleTap_Handler :(UITapGestureRecognizer *)GR
{
NSLog(@"Hi! You just touched the screen!");
}
When I compiled and deployed the project into my iPad3, every thing worked just as planned until the Touch event, which didn’t work.
I got the following messages printed out to the console window:
2012-08-26 14:03:59.589 Ex_TouchEvents_01[806:707] Hi! ViewController's viewDidLoad msg sent!
2012-08-26 14:03:59.593 Ex_TouchEvents_01[806:707] [TouchEvent_View initWithFram] sent!
But I did not see the “Hi, you just touched the screen!” message after I touched the screen. In addition, I didn’t see the background of the View area set to blue either. So, I must have been missing something that was very simple. I have been googling all over the web, but I couldn’t figure out what I missed. Would some body kindly point out what I have done wrong?
I don’t have access to my Mac to test this, but I believe you’re missing a colon in this line.
It needs to be:
If a method takes any parameters, the colon(s) are part of the selector.