Manually Switching First Responder Status Between Views


#1

Following the code provided in this chapter, shaking the device will change the stroke color from light gray to red - but only, it seems, for the first instance of the view. For example, when exploring UIScrollView by placing two identical instances of the view side-by-side, shaking the device will change the circles to red on the first view (i.e., the one at origin 0,0), but not the second view (i.e., the one at origin [screenRect.size.width], 0). Clearly this occurs because that first view is set as the first responder in the code. However, how would I get around this? If I wanted the shake to change BOTH views (i.e. have a motion event affect two objects simultaneously), how could I do this?

If I do this:

[code] // try to make first view the first responder; log
BOOL success = [view becomeFirstResponder];
if (success)
{
NSLog(@“First HypnosisView became first responder”);
}
else
{
NSLog(@“Could not become first responder”);
}

// make the second view the first responder; log
BOOL successTwo = [anotherView becomeFirstResponder];
if (successTwo)
{
    NSLog(@"Second HypnosisView became first responder");
}
else
{
    NSLog(@"Could not become first responder");
}[/code]

That simply changes the first responder to the second view, in which case, the second view turns red, but the first does not. A Google search along the lines of “set first responder to multiple views” proved fruitless. Any thoughts?


#2

Incidentally, I tried (separately) creating an enum and an NSArray of the two views, and tried setting the resulting list to be the first responder. That caused all sorts of problems.


#3

Possible solutions:

  • Have the view receiving the shake-events send out a notification that other views can act on.
  • Get your AppDelegate to receive device-orientation notifications to inform the relevant views of the shake events.

See:

  • NSNotificationCenter Class Reference
  • UIDevice Class Reference

#4

I am wondering the same thing.

Understanding MVC might shed some light. The next chapter enters into View Controllers and in that design pattern a controller could switch responder status between views. Views ideally would not talk to each other as to not rely on each other but a controller can talk to multiple views.

Not a solution but just a theory.