UIButton addTarget 不在调用选择器中

发布于 12-12 08:32 字数 2178 浏览 0 评论 0原文

首先我要声明,这不是正确选择选择器名称的问题,也不是在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

握住你手2024-12-19 08:32:49

loadView 是否被调用过?在 loadView 中放置一个 NSLog (或断点),作为健全性检查。

另外,来自 UIViewController文档

如果您重写此方法 (loadView) 以手动创建视图,
您应该这样做并将层次结构的根视图分配给
查看属性。 (您创建的视图应该是唯一的
实例,不应与任何其他视图控制器共享
对象。)此方法的自定义实现不应调用super

如果您想对视图执行任何其他初始化,请执行以下操作
所以在 viewDidLoad 方法中。
在 iOS 3.0 及更高版本中,您
还应该重写 viewDidUnload 方法来释放任何
对视图或其内容的引用。

尝试将所有按钮代码放入 viewDidLoad 中:

- (void)buttonResponse:(id)sender {
    NSLog(@"Button touched up inside!!!!!");
}

- (void)loadView {
    // Dont call super (according to the UIViewController docs)
    // [super loadView];

    // self.view = ... however your setting up self.view 
    // self.view.frame = CGRectMake....
    // self.view.etc...
    [self.view setNeedsDisplay];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    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];

    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];    
}

- (void)dealloc {
    [button release];
    [super dealloc];
}

Does loadView ever get called? Put a NSLog (or a breakpoint) in loadView just as a sanity check.

Also, from UIViewController Documentation:

If you override this method (loadView) in order to create your views manually,
you should do so and assign the root view of your hierarchy to the
view property. (The views you create should be unique
instances and should not be shared with any other view controller
object.) Your custom implementation of this method should not call super.

If you want to perform any additional initialization of your views, do
so in the viewDidLoad method.
In iOS 3.0 and later, you
should also override the viewDidUnload method to release any
references to the view or its contents.

Try putting all the button code in viewDidLoad:

- (void)buttonResponse:(id)sender {
    NSLog(@"Button touched up inside!!!!!");
}

- (void)loadView {
    // Dont call super (according to the UIViewController docs)
    // [super loadView];

    // self.view = ... however your setting up self.view 
    // self.view.frame = CGRectMake....
    // self.view.etc...
    [self.view setNeedsDisplay];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    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];

    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];    
}

- (void)dealloc {
    [button release];
    [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文