无法从UIButton获取UITouch事件

时间:2012-12-31 01:04:09

标签: objective-c ios xcode cocoa-touch user-interface

问题:

我无法让我的UIButton发送我的班级活动电话

这是我先试过的:

我创建了一个扩展UIViewController的类,名为Viewer

#import "Viewer.h"
#import "MainScreen.h"

@implementation Viewer

- (void)viewDidLoad
{
    MainScreen* main = [[MainScreen alloc]init: self];
    [main show];

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

然后:

  • 我创建了一个名为MainScreen的类,2个IBOutlets - singlePlayerButton和view
  • Viewer是UIViewController的实例
  • buttonPressed的返回类型为IBAction

MainScreen:

#import "MainScreen.h"
#import "Viewer.h"

@implementation MainScreen
{
    IBOutlet UIButton* singlePlayerButton;
    IBOutlet UIView* view;

    Viewer* viewer;
}

- (id)init:(Viewer*) theViewer
{
    self = [super init];
    viewer = theViewer;
    return self;
}

- (void)show
{
    [[NSBundle mainBundle] loadNibNamed:@"MainScreenLayout" owner:self options:nil];
    [viewer.view addSubview:view];
}

- (IBAction)buttonPressed:(id)sender
{
    NSLog(@"A button was pressed!");
}

@end

然后我创建了一个名为MainScreenLayout.xib

的NIB

这是它的样子:

Screenshot of my NIB file

正如你所看到的,我有四个按钮,但唯一一个连接到任何东西的是单人播放器按钮我也连接到视图的UIView。

现在,我将动作链接到单人游戏按钮,如下所示:

Screenshot of connecting the action to the button

buttonPressed是我按下按钮时要调用的方法,所以我按住了First Responder,然后点击buttonPressed旁边的圆圈,然后将其拖到顶部单人游戏按钮。然后弹出下一个对话框窗口,我点击了Touch up Inside。以下是“已接收操作”窗口的外观:

Screenshot of First Responder Window

现在,我认为它应该有效。我在iPhone模拟器上运行它并按下按钮。

这样就没有问题我是否能胜任,这是我实际点击按钮的图片:

Screenshot

我没有任何输出。然后我转到我的代码并查看了该方法。这就是它的样子:

Screenshot of method

如您所见,椭圆未填充,意味着该方法未配对。所以很自然地我认为它被另一种方法所覆盖。所以这就是现在的方法:

- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender
{
    NSLog(@"A button was pressed!");
}

然后我回到我的NIB窗口,取消配对我的单人播放按钮,然后将其修复为aVerySpecificMethodNameForMyButtonListener。现在这就是它的样子:

Screenshot for rediculous method name

然后当我按下按钮时,我再次运行它仍然没有。然后我回到了代码:

Screen shot

(在这里插入挫折)

此时我重新启动了Xcode和模拟器并再次查看了所有内容,但仍然无效。

这是我接下来尝试过的,这看起来简单了十亿倍:

- (void)show
{
    [[NSBundle mainBundle] loadNibNamed:@"MainScreenLayout" owner:self options:nil];
    [viewer.view addSubview:view];

    [singlePlayerButton addTarget:self action:@selector(aVerySpecificMethodNameForMyButtonListener:) forControlEvents:UIControlEventTouchUpInside];
}

我用我的show方法替换了上面的方法。唯一不同的是最后一行。我将方法与NIB中的单播者按钮断开连接。然后我跑了它,我兴奋了一秒钟,因为我有一些输出。这是我得到的:

(lldb) 

非常有用的输出C调试器,一如既往。在接下来的几个小时里,我试图找出我做错了什么。我尝试了无数的例子,但没有一个能够奏效。

有人在这看到我做错了什么吗?难道只是Xcode才变得潇洒?

感谢阅读!!欢迎所有相关答案!


更新:

我还尝试将UIButton的声明更改为Header文件中的属性:

#import <Foundation/Foundation.h>
#import "Viewer.h"

@interface MainScreen : NSObject
{
    IBOutlet UIView* view;

    Viewer* viewer;
}

@property (weak, nonatomic) IBOutlet UIButton* singlePlayerButton;

- (id)init:(Viewer*) theViewer;
- (void)show;
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender;

@end

因此,我在MainScreen.m中更改了以下内容:

#import "MainScreen.h"
#import "Viewer.h"

@implementation MainScreen

- (id)init:(Viewer*) theViewer
{
    self = [super init];
    viewer = theViewer;
    return self;
}

@synthesize singlePlayerButton;

- (void)show
{
    [[NSBundle mainBundle] loadNibNamed:@"MainScreenLayout" owner:self options:nil];
    [viewer.view addSubview:view];

    [singlePlayerButton addTarget:self action:@selector(aVerySpecificMethodNameForMyButtonListener:) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender
{
    NSLog(@"A button was pressed!");
}

@end

现在MainScreen.m文件中没有变量声明,我将它们全部移动到Header。 singlePlayerButtonview旁边的圆圈仍然填充,但方法旁边的气泡不是。我再次运行代码并获得相同的输出。

1 个答案:

答案 0 :(得分:1)

所以我已经把它放在评论中,因为它真的只是一个建议但是我没有代表那么抱歉让你的希望,但我相信触摸事件的范围只是在它发生的UIView里面。当触摸事件被触发时,它不会被传递给UIView的处理程序(MainScreen类),因此事件的侦听器方法不会被调用...但是你能够在界面构建器中将它链接到按钮是我很困惑,所以我可以离开。

无论如何,我认为你应该去的地方是让你的MainScreen成为一个UIView对象,看看它对于视图中按钮的事件处理有什么作用。