UIButton addTarget 不在调用选择器中
首先我要声明,这不是正确选择选择器名称的问题,也不是在 loadView 中设置按钮目标的问题,也不是我在过去 4 个小时的浏览中看到的任何其他建议的问题。我还应该解释一下我没有使用笔尖,也没有使用 IB。
这些按钮在大部分开发过程中都发挥了作用。
然后噗!这不仅仅是按钮。我的表格视图不滚动。按表视图中的单元格不会调用我设置的操作。
看来我已经完全打破了响应者链;或类似的东西。
为了尝试缩小问题的根源,创建了一个基于窗口的项目;去掉 xCode 生成的所有内容;删除了笔尖;并从我的原始应用程序委托和 rootViewController 中复制了多个方法。
它是包含子视图(包含按钮)的 RVT。相当直接。效果一样。我实际上已经删除了所有功能代码,但 appDelegate.m: 中的以下源代码:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setDelegate:self];
if(window == nil)
window = [[UIWindow alloc] init];
window.autoresizesSubviews = YES;
if(controller == nil)
controller = [[Controller alloc] init];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
}
-(void)dealloc {
[window release];
[controller release];
[super dealloc];
}
和 Controller:
- (void)buttonResponse {
NSLog(@"Button touched up inside!!!!!");
}
- (void)loadView {
[super loadView];
//test
button = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
CGSize imageSize = [image size];
CGRect buttonFrame = CGRectMake(0,0,imageSize.width + 100, BUTTONBAR_H + 100);
button.frame = buttonFrame;
[button setImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"" forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(buttonResponse) forControlEvents: UIControlEventTouchUpInside];
UIView *view = self.view;
CGRect viewFrame = view.frame; //self.view is valid and takes up available screen
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
//end test
[self.view setNeedsDisplay];
}
- (void)dealloc {
[button release];
[super dealloc];
}
我知道它一定是非常简单的东西。但我看不到它。我宁愿把头发留在原处。我想知道随着我继续学习这些怪癖,这种情况是否会发生。
Let me first state, that this is not a matter of getting my selector name right, nor setting up the button target in loadView, nor any other suggestion I've seen in the last 4 hours of browsing. I should also explain I am not using nibs, nor IB.
These buttons have been working for the bulk of development.
Then poof! It's not just the buttons. My table views do not scroll. Pressing the cells in the table views do not invoke the actions that I had setup.
It seems I've broken the responder chain entirely; or something to that effect.
To try and narrow down the source of the problem a created a window-based project; stripped out everything that xCode generates; deleted the nib; and copied in several methods from my original app delegate and my rootViewController.
It's the RVT that contains the subView that contains the buttons. Fairly straight forward. Same effect. I've literally stripped out all functional code but the following source in appDelegate.m:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setDelegate:self];
if(window == nil)
window = [[UIWindow alloc] init];
window.autoresizesSubviews = YES;
if(controller == nil)
controller = [[Controller alloc] init];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
}
-(void)dealloc {
[window release];
[controller release];
[super dealloc];
}
And in Controller:
- (void)buttonResponse {
NSLog(@"Button touched up inside!!!!!");
}
- (void)loadView {
[super loadView];
//test
button = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
CGSize imageSize = [image size];
CGRect buttonFrame = CGRectMake(0,0,imageSize.width + 100, BUTTONBAR_H + 100);
button.frame = buttonFrame;
[button setImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"" forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(buttonResponse) forControlEvents: UIControlEventTouchUpInside];
UIView *view = self.view;
CGRect viewFrame = view.frame; //self.view is valid and takes up available screen
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
//end test
[self.view setNeedsDisplay];
}
- (void)dealloc {
[button release];
[super dealloc];
}
I know it's gotta be something really simple. But I can't see it. I would prefer to leave my hair in place. I'm wondering if that's going to happen as I continue to learn the quirks.
loadView
是否被调用过?在loadView
中放置一个NSLog
(或断点),作为健全性检查。另外,来自
UIViewController
文档:尝试将所有按钮代码放入
viewDidLoad
中:Does
loadView
ever get called? Put aNSLog
(or a breakpoint) inloadView
just as a sanity check.Also, from
UIViewController
Documentation:Try putting all the button code in
viewDidLoad
: