iPhone SDK新手:未调用touchesBegan

时间:2009-10-26 00:05:57

标签: iphone-sdk-3.0 uiview uiviewcontroller

我使用File / New Projet / View-Based应用程序创建了一个非常基本的iPhone应用程序。 那里没有NIB文件。

这是我的appDelegate

·H

    @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    MyViewController *viewController;
}

的.m

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

这是我的控制器中的loadView方法

 - (void)loadView {
 CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
 UIView *contentView = [[UIView alloc] initWithFrame:mainFrame];
 contentView.backgroundColor = [UIColor redColor];
 self.view = contentView;
 [contentView release];
}

现在,为了捕获touchesBegan事件,我创建了一个新的UIView子类:

·H

@interface TouchView : UIView {
}

的.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 NSLog(@"Touch detected");

}

并将loadView中的第二行修改为:

 TouchView *contentView = [[UIView alloc] initWithFrame:mainFrame];

为什么touchesBegan从未打过电话?

2 个答案:

答案 0 :(得分:3)

如果您将loadView更改为:

TouchView *contentView = [[TouchView alloc] initWithFrame:mainFrame];

在TouchView中捕捉触摸应该没有问题。在您的代码中,您没有创建TouchView实例,而是创建了一个UIView实例。

答案 1 :(得分:0)

找到解决方案:touchesBegan在viewController上调用,而不是在view ...

上调用
相关问题