【问题标题】:UIButton addTarget does not in invoke selectorUIButton addTarget 不在调用选择器中
【发布时间】:2011-10-30 04:08:43
【问题描述】:

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

在控制器中:

- (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];
}

我知道这一定很简单。但我看不到它。我宁愿把头发留在原处。我想知道随着我继续学习这些怪癖,这是否会发生。

【问题讨论】:

    标签: ipad uibutton


    【解决方案1】:

    loadView 是否曾经被调用?将NSLog(或断点)放在loadView 中,作为健全性检查。

    另外,来自UIViewController Documentation

    如果您覆盖此方法 (loadView) 以手动创建视图, 您应该这样做并将层次结构的根视图分配给 view 财产。 (您创建的视图应该是唯一的 实例,不应与任何其他视图控制器共享 对象。)此方法的自定义实现不应调用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];
    }
    

    【讨论】:

    • 你好。感谢您的回复。 loadView 实际上被调用了。我发现同时定义 loadView 和 viewDidLoad 会破坏我的调用堆栈。我还尝试将 loadView 的内容移动到 viewDidLoad,并省略 loadView。相同的明显效果...
    • 我想省略 [super loadview] 并用框架分配 self.view 是定义 loadview 的正确方法。仍然没有任何变化......
    • 这很奇怪。尝试输入NSLog(@"%@", [button allControlEvents]);NSLog(@"%@", [button allTargets]);,看看按钮是否有正确的操作。
    • EXC_BAD_ACCESS 首先。第二次没有触摸事件。
    • 当您将 UIControlEvents 作为 %@ 传递时,显然是 NSLog 对象。它评估为 64,即 UIControlEventTouchUpInside。
    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多