我不能让我的UIButton送我的一类事件调用
这是我第一次尝试:
我做了一个类其延伸的UIViewController,称为查看器
#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和视图
- 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
这是它看起来像E:
正如你所看到的,我有四个按钮,但连接到任何东西只有一个是单人游戏按钮,我也UIView的连接查看。现在
,我挂像这样的动作单人游戏按钮:
buttonPressed
是我的方法,我想在按下按钮时呼吁,所以我CTRL-点击First Responder,然后点击buttonPressed
旁边的圆圈,然后将其拖到单人播放按钮的顶部。然后弹出下一个对话窗口,我点击Touch up Inside。以下是收稿操作窗口的样子:
现在,我想在这一点上它应该工作。我在iPhone模拟器上运行它并按下按钮。
只是所以没有往哪里去的问题或不是我能干,这里是我的实际点击按钮的图片:
我没有任何输出。然后我去了我的代码并查看了方法。这是什么样子:
,你可以看到,椭圆形不填充,这意味着该方法不配对。所以自然我认为它被另一种方法所覆盖。因此,这里有什么方法貌似现在:
- (IBAction)aVerySpecificMethodNameForMyButtonListener:(id)sender
{
NSLog(@"A button was pressed!");
}
然后我又回到了我的NIB窗口,不成对我的单人游戏按钮,然后将它修好aVerySpecificMethodNameForMyButtonListener
。现在,这是什么样子:
然后我又跑了,仍然一无所获,当我按下按钮。然后我又回到了代码:
(此处插入的挫折)
在这一点上,我重新启动Xcode和模拟器和看着一切再次,它仍然没有工作。
这是我尝试下一个,这似乎大约十亿倍以上简单:
我与我的show
方法与上面的一个替代。唯一不同的是最后一行。我从NIB中的单人按钮断开了我的方法。然后我跑了它,我因为获得了一些输出而兴奋了一秒钟。这里是我得到的:
(lldb)
非常有帮助的输出C调试器,一如既往。我花了接下来的几个小时,试图找出我做错了什么。我已经尝试了无数的例子,但都没有成功。
有没有人看到我在这里做错了?它只是Xcode是flakey?
感谢您的阅读!欢迎所有相关答案!
UPDATE:
我也尝试过改变的UIButton的声明在头文件的属性:
#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文件中没有变量声明,我将它们移动了al l到标题。 singlePlayerButton
和view
旁边的圆圈仍然填充,但该方法旁边的气泡不是。我再次运行代码并获得相同的输出。
在你最后一次尝试得到这个工作时,你是否在头文件中声明了'UIButton'属性?应该像'@property(弱,非原子)IBOutlet UIButton * singlePlayerButton;'。您还需要将按钮连接到此声明(填充旁边的圆圈)。然后勾选动作,需要将该按钮引用为'self.singlePlayerButton addTarget ...' – bobnoble
@bobnoble'singlePlayerButton'和'view'旁边的气泡都被填充。我的方法旁边的按钮没有被填充。现在,我只是将UIButton定义在'MainScreen'实现的大括号内。我会将其添加为属性,然后我会更新我的帖子。好主意。 – John