tags:

views:

352

answers:

3

Hey all, I'm completely stumped with this iPhone problem.

This is my first time building a view programmatically, without a nib. I can get the view displaying things just fine, but the darn ViewController isn't responding to touches the way it used to in programs where I used a nib. I should add that in the past, I started with the View-Based Application template, and this time I used the Window-Based Application template.

My thinking is that the View-Based template does something magical to let the iPhone know where to send the touch events, but I can't figure out what that would be even after several hours of bumbling around Google. Or I could be looking in an entirely wrong place and my troubles are related to something else entirely. Any thoughts?

A: 

There's nothing magical in the view-based template. The most likely reasons for failure to respond to touches are:

  • You've messed with touchesBegan:withEvent:, userInteractionEnabled, exclusiveTouch or something else, thinking you need to mess with these (generally you don't; the defaults are generally correct)

  • You created a second UIWindow

  • You put something over the view (even if it's transparent)

Simplify your code down to just creating a view programatically that responds to a touch and nothing else. It should be just a few lines of code. If you can't get that working, post the code and we'll look at what's going on.

Rob Napier
A: 

I went ahead and created a new project, and created a subclass of UIView and UIViewController along with it. I then added an instance of the ViewController to the delegate's header (along with an @property and @synthesize) and added this to applicationDidFinishLaunching

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

// Override point for customization after application launch
viewController = [[MyViewController alloc] init];//added
[window addSubview:viewController.view];//added
[window makeKeyAndVisible];

}

MyView only has initWithFrame implemented to set the background color to orange

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    // Initialization code
 self.backgroundColor = [UIColor orangeColor];
}
return self;
}

Lastly, the ViewController looks as such

- (void)loadView {
self.view = [[MyView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}



- (void)viewDidLoad {
[super viewDidLoad];
 }


-(void) touchedEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
NSLog(@"Touches ended");
}

I get a pretty orange screen, but nothing showing up in the console when I poke at it. In the project I've been working on, I haven't fiddled with touchesBegan, userInteractionEnabled, or exclusiveTouch, nor did I create a second UIWindow. I did draw an image in the view as the background, but even without doing that in the above code, I'm still not getting touches to respond properly. Which must mean I'm missing something else. It's probably staring me right in the face, too.

A: 

Problem solved. touche**s**Ended != touche**d**Ended.

That'll teach me to program without my glasses on.