【问题标题】:New to iPhone SDK: touchesBegan not callediPhone SDK 的新功能:未调用 touchesBegan
【发布时间】:2009-10-26 00:05:56
【问题描述】:

我使用 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?

【问题讨论】:

    标签: iphone-sdk-3.0 uiview uiviewcontroller


    【解决方案1】:

    如果你把 loadView 改成这样:

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

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

    【讨论】:

      【解决方案2】:

      找到解决方案:touchesBegan 在 vi​​ewController 上被调用,而不是在视图上......

      【讨论】:

      • 非常接近。显然,UIViewController UIView 之前获取 touchesBegan/touchesEnded/etc 消息。默认情况下,UIViewController 不会将消息传递给 UIView(通过调用 [super touchesBegan])。因此,如果您的 UIView 有控制器,您需要移动或前进到 touches* 功能。它令人困惑并且从未被正确记录,但这是传统的可可方式。
      • 那是绝对错误的伙伴......它也被称为 UIView 类......developer.apple.com/library/ios/#documentation/UIKit/Reference/…
      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多