无法返回firstResponder状态

时间:2012-04-28 17:40:08

标签: objective-c first-responder

设置我的appDelegate以返回firstResponder状态,但是当我编译(编译正常)时,控制台返回“无法成为第一个响应者”,但没有告诉原因。

我附上了代码。你能告诉我什么是错的吗?提前谢谢。

HypnosisterAppDelegate.m

#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"

@implementation HypnosisterAppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

  HypnosisView *view = [[HypnosisView alloc] initWithFrame:[[self window]bounds]];
  [[self window]addSubview:view];

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

  self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

-(BOOL)canBecomeFirstResponder
{
  return YES;
}

1 个答案:

答案 0 :(得分:3)

您已在HypnosisterAppDelegate上实现了canBecomeFirstResponder,它不是UIResponder子类。然后将becomeFirstResponder发送到自定义视图(HypnosisView)。

HypnosisView(不是HypnosisterAppDelegate)需要这样做:

-(BOOL)canBecomeFirstResponder
{
  return YES;
}