让我首先说明,这不是让我的选择器名称正确,也不是在loadView中设置按钮目标,也不是我在过去4小时的浏览中看到的任何其他建议。我还应该解释一下,我不是在使用笔尖,也不是IB。
这些按钮一直用于大部分开发 然后噗!这不仅仅是按钮。我的表格视图不滚动。按表视图中的单元格不会调用我已设置的操作。
似乎我完全打破了响应者链;或类似的东西。
为了尝试缩小问题的根源,创建了一个基于窗口的项目;剥离了xCode生成的所有内容;删除了笔尖;并从我原来的app delegate和我的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];
}
我知道这一定非常简单。但我看不到它。我宁愿把头发留在原处。我想知道这是否会发生,因为我继续学习这些怪癖。
答案 0 :(得分: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];
}