【发布时间】:2012-07-21 20:47:27
【问题描述】:
我在link 中尝试了 Moshe 的代码,除了“for (UIButton *button in ...”) 部分之外,它都可以正常工作,并且每次单击按钮时都会崩溃。
所以我在 viewDidLoad 方法中尝试了这段代码:
UIButton *testButton = [[UIButton alloc]initWithFrame:CGRectMake(20,50,30,30)];
testButton.backgroundColor = [UIColor orangeColor];
[testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButton setTitle:@"A" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(commonMethodForButtons:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:testButton];
[testButton release];
我的项目只包含这个和 Moshe 的示例代码。知道应用程序崩溃的原因吗?我没有收到崩溃日志。
编辑:
在开放范围内我有这个方法:
-(void)commonMethodForButtons:(id)sender
{
NSLog (@"you touched me!");
}
编辑 2:
我找到了这个问题的原因:
我在 AppDelegate 中注释掉了 [mvc release];,所以它现在可以完美运行了 :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MVC *mcv = [[MVC alloc]initWithNibName:nil bundle:nil];
[self.window addSubview: mcv.view];
//[mcv release];
[self.window makeKeyAndVisible];
return YES;
}
感谢您指出这一点! :)
【问题讨论】:
-
你有实现 commonMethodForButtons: 方法吗?
-
@vladimir 我在编辑中添加了代码。这个方法在你问之前就已经存在了。
-
尝试设置异常断点或检查控制台是否有错误消息可能是......到目前为止,您的代码看起来还不错。
-
你现在的班级是什么?是视图控制器吗?
-
抱歉,将“strong”替换为“retain”