2012-07-21 41 views
0

我試過了這個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; 
} 

謝謝你指出這一點! :)

+1

你有commonMethodForButtons:方法來實現? – Vladimir 2012-07-21 20:50:06

+0

@vladimir我在編輯中添加了代碼。這個方法在你問之前就在那裏。 – wagashi 2012-07-21 21:12:43

+0

嘗試設置異常斷點或檢查控制檯錯誤消息可能是...你的代碼看起來很好至今。 – Vladimir 2012-07-21 21:16:11

回答

1

使用MCV財產

在AppDelegate中的頭文件:

@class MVC; 
    @interface AppDelegate : UIResponder { 
    MVC *mcv; 
} 

@property (nonatomic, retain) MVC *mcv; 

在實現文件

@implementation AppDelegate 

@synthesize mcv; 

- (void)dealloc 
{ 
    [mcv release]; 
    [super dealloc]; 
} 
+0

嗯,我得到了在頭文件中的警告:'錯誤:在'MVC''之前預期的說明符 - 限定符列表我有@interface xxxAppDelegate:NSObject {... – wagashi 2012-07-22 13:34:58

+0

發佈代碼,請你忘了東西 – 2012-07-22 14:01:31

+1

@teofile我找到了解決方案。請看看這個http://stackoverflow.com/questions/7428706/error-expected-specifier-qualifier-list-before-xxx我得到這個警告,因爲它是週期性的依賴。我在頭文件中的'@interface AppDelegate ...'前面加了'@class MVC;'。順便說一句,你很可愛;) – wagashi 2012-07-23 11:23:25

0

不,你應該來解決您的通過註釋掉[mcv release](這是隻有固定的症狀,而不是問題,而且會造成問題,如果你曾經轉換爲ARC,W問題HICH你應該)也不應該知道,作爲目前公認的答案,可以存儲mcv(應該不是是mvc?)作爲應用程序的委託,但(一)未按照規定rootViewController但(b)仍然使用addSubview的伊娃。您應該使用應用程序代理的rootViewController。當前的解決方案不會正確配置您的視圖控制器層次結構它應該是如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch. 

    MVC *mvc = [[MVC alloc]initWithNibName:nil bundle:nil]; // really no NIB name?!? 

    //[self.window addSubview: mvc.view]; 
    self.window.rootViewController = mvc; 

    [mvc release]; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

或者,更好的,符合由非ARC的Xcode模板生成標準didFinishLaunchingWithOptions,如:

// AppDelegate.h 

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) ViewController *viewController; 

@end 

而且

// AppDelegate.m 

#import "AppDelegate.h" 

#import "MVC.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController = _viewController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch. 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController = [[MVC alloc]initWithNibName:nil bundle:nil]; // really no NIB name?!? 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

我從來沒有過引用這個viewController財產,但考慮到這是默認的代碼生成的Xcode,我認爲這是很好的做法。