Question

In my view controller I have initialized a new UIView Subclass called HypnosisView and added it to the root view of the ViewController. And in viewDidLoad I set the view to become the first responder like so:

- (void)viewDidLoad
{
    [super viewDidLoad];
    HypnosisView *view2 = [[HypnosisView alloc] initWithFrame:[[self view] bounds]];
    [self.view addSubview:view2];

    BOOL success = [view2 becomeFirstResponder];
    if (success) {
        NSLog(@"HypnosisView became the first responder");
    } else {
        NSLog(@"Could not become first responder");
    }
}

I also overrode the method canBecomeFirstResponder in the UIView HypnosisView.m file like so:

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

Now when I run the program I am expecting the console to report "HypnosisView became the first responder" but it reports "Could not become first responder."

BUT

when I shake the device the view does respond to the shake event! So it looks like it actually became the first responder but it is reporting it did not become first responder. Such a strange issue, please help!

Was it helpful?

Solution

Make view2 a property within your UIViewController. Create view2 and add it to the subview in -viewDidLoad, but set it as the first responder in -viewDidAppear:. I am not certain of this, but I assume the UIViewController's view has to appear before it or any of its subviews can become first responder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top