1

I have a subview, and part of the view is transparent, so to the user, if they were to touch in that transparent space then whatever is visible (but also underneath it) should be interactive.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
if (CGRectContainsPoint(tableViewUse.frame, pt)){
    [self becomeFirstResponder];
}else {
    [super becomeFirstResponder];
}    
}

I have the above code in my subview class, but the problem is that the [super becomeFirstResponder] call is not working. Now the obvious solution is to have a [self resignFirstResponder] call in the class of my superview; however, I plan to use many instances of this class in my code, so I would have to find the touch and compare it against the frame of each of my instances etc. So the elegant solution is to control everything from the subview.

Thank you for any help!

P.s just noticed a problem that will change my question. If I were to make the superview become the first responder, then any touchesBegan method will be called in that superview and the touch will have to be managed there. eeeeeek.

prince
  • 506
  • 6
  • 18
  • Have you checked to see if `NSLog(@"%@",self.superView");` returns the desired output? Also, what do you mean by `[super becomeFirstResponder` "is not working?" _How_ is it failing? – tacos_tacos_tacos Apr 20 '12 at 14:00
  • Yes, and it does. It's not working as in: if the code wasn't there, exactly the same behaviour would be exhibited. – prince Apr 20 '12 at 14:03
  • Is your superview the same kind of object as the subview you are referencing? What is going on in the superview? In the view controller that manages the superview are you using `- (BOOL)canBecomeFirstResponder { return YES; }`? – tacos_tacos_tacos Apr 20 '12 at 14:09
  • Also, please see http://stackoverflow.com/questions/1469876/uiviewcontroller-cant-become-first-responder . – tacos_tacos_tacos Apr 20 '12 at 14:10
  • Okay, I see where I'm going wrong now. But I can't set any object in the superview to become the first responser because I'd have to create an instance of the superview within my subview, which is silly. – prince Apr 20 '12 at 14:17
  • Have you considered having your View Controller manage these things, using an array of subviews? ... It can become the first responder and delegate that responsibility accordingly once the subview signals... – tacos_tacos_tacos Apr 20 '12 at 14:20
  • I was considering the delegate as a means to do it, since I also have a UINavigationController. However this will create a lot of problems for me. I have thought of a solution but it is completely out of the context of this question.... – prince Apr 20 '12 at 14:40

1 Answers1

2

Tried this

[self.superview becomeFirstResponder]; ?

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
unexpectedvalue
  • 6,079
  • 3
  • 38
  • 62
  • Now I've found a problem with my logic, see the updated original question. – prince Apr 20 '12 at 13:50
  • @JamesPrince regardless of the problem in your logic, `[super resignFirstResponder]` is wrong. You are confusing the superclass and the super view, which are entirely different things. – tacos_tacos_tacos Apr 20 '12 at 13:54
  • I know that now, I've tried what ssteinberg suggested, but it gives me the same behaviour - as I've pointed out in the first comment I made. – prince Apr 20 '12 at 14:00