Why won't my custom view become First-Responder, iOS?

巧了我就是萌 提交于 2019-12-10 17:33:45

问题


I am following the book, iOS Programming Big Nerd Ranch Guide, and I have come to a lesson where I am to create a custom view, HypnosisView. Now, I am suppose to have this view change it's color on shake, but it says I am suppose to make it the First-Responder.

I used,

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

and

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

However, whenever I run my app, it always says that it could not become the first responder.

Any help would be appreciated.

UPDATE

I forgot to mention I get this output message.

Application windows are expected to have a root view controller at the end of application launch


回答1:


Alright. I figured it out. I needed to put the delegate method

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

In the CustomView.m file, not my App Delegate file. Easy fix.



来源:https://stackoverflow.com/questions/12691075/why-wont-my-custom-view-become-first-responder-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!