为什么我的自定义视图不会成为 iOS 的第一响应者? [英] Why won't my custom view become First-Responder, iOS?

查看:31
本文介绍了为什么我的自定义视图不会成为 iOS 的第一响应者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习《iOS 编程大书呆子牧场指南》这本书,并且我已经开始学习创建自定义视图 HypnosisView 的课程.现在,我想让这个视图在抖动时改变它的颜色,但它说我想让它成为第一响应者.

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.

我用过,

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

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.

任何帮助将不胜感激.

更新

我忘了提到我收到了这个输出消息.

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

推荐答案

好的.我想到了.我需要放置委托方法

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

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

在 CustomView.m 文件中,而不是我的 App Delegate 文件.轻松修复.

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

这篇关于为什么我的自定义视图不会成为 iOS 的第一响应者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆