我試過了這個link中Moshe的代碼,它除了「for(UIButton * button in ...」的部分,每次點擊一個按鈕時都會崩潰iOS - UIButton事件崩潰
所以,我想這個代碼在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's樣本代碼的任何想法,爲什麼應用程序崩潰我沒有得到任何崩潰日誌
編輯:
在開放範圍我有這樣的方法:
-(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 2012-07-21 20:50:06
@vladimir我在編輯中添加了代碼。這個方法在你問之前就在那裏。 – wagashi 2012-07-21 21:12:43
嘗試設置異常斷點或檢查控制檯錯誤消息可能是...你的代碼看起來很好至今。 – Vladimir 2012-07-21 21:16:11