2011-10-29 3 views
1

X-Code 4.2を使用してアプリケーションを構築し始めたばかりです。私は単一のビューのアプリケーションテンプレートでアプリケーションを作成しました。ボタンを押した後Main.mでクラッシュする

私は画像、ラベル、ボタンを含む単純なVCであるMainIconViewControllerを作成しました。私は私のmainViewControllerビューにMainIconViewControllerビューを追加し、ボタンが押されたとき、私は言うMain.mでクラッシュを取得:

- [__ NSCFType mainButtonPressed:]:認識されていないセレクタはインスタンス0xb867ba0

ラインでに送信クラッシュが発生しMain.mがされています。今のbuttonPressed

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 

:完全に空です。

これを修正する方法はありますか?

これを再テストするための全く新しいプロジェクトを作成しようとしました。私はプロジェクトを作成し、単一のボタンを含むviewControllerサブクラスを作成しました。次に、メインビューコントローラでこのビューのインスタンスを作成し、それをビューに追加します。ボタンを押すと、前と同じようにアプリケーションがクラッシュします。

EDIT: はここにすべてのコードです:

//MainIconViewController.h 
@protocol MainIconViewControllerDelegate <NSObject> 

-(void) openFileNamed:(NSString *)name; 
-(void) createNewFile; 

@end 

#import <UIKit/UIKit.h> 

@interface MainIconViewController : UIViewController { 


} 
@property (assign) id <MainIconViewControllerDelegate> delegate; 
@property (weak, nonatomic) IBOutlet UILabel *titleLabel; 
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
- (void)mainButtonPressed:(id)sender; 


@end 


////////////////////////////// 
//MainIconViewController.m 
#import "MainIconViewController.h" 

@implementation MainIconViewController 
@synthesize titleLabel; 
@synthesize imageView; 
@synthesize delegate; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [self setTitleLabel:nil]; 
    [self setImageView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 
- (void)mainButtonPressed:(id)sender{ 

} 

@end 
+1

実際には、main()にクラッシュがありませんでした。ボタンアクションコードを投稿してください。 – zaph

+0

ボタンアクションには何もありません – Brodie

答えて

0

まず、あなたの方法は次のように宣言する必要があります。

- (IBAction)mainButtonPressed:(id)sender; 

その後、あなたはこのようなあなたのコントローラーを呼び出す必要があります:安全な(

MainIconViewController *icon = [[MainIconViewController alloc]initWithNibName:@"your xib name without extension" bundle:nil]; 

が、あなたのXIBファイルが同様に命名されている場合それは大丈夫ですコントローラー)

もちろん、アクションボタンを使ってボタンをIBにリンクすることを忘れないでください。

MainIconViewControllerに対応するxibのFile's owner(IBに)がMainIconViewControllerに設定されていることも確認してください。

ここではメモリリークがあることに注意してください。あなたはあなたのクラスにコントローラを残して、もはや必要ではないそれを解放するかもしれません。リリースが早すぎると、必要に応じてビューにメモリのコントローラがないため、クラッシュします。リリースされていない場合(ここのように)、コントローラはメモリにとどまり続け、メモリリークが発生します。

あなたのやり方は、Apple(1つのビュー階層= 1つのビューコントローラ)によって推奨されていないことに注意してください。 ARCを使用すると、クラッシュの問題になる可能性があります。

+0

エラーに私を導いていただきありがとうございます - 何らかの理由でARCが言語でオフになっていて、私のターゲットの警告でオンになっています。私は両方をオンにして、それは正常に動作します。 – Brodie

0

としては、エラー出力に、あなたは、ヘッダーファイル内のメソッドmainButtonPressed:を宣言していないと述べました。あなたのヘッダファイルに

- (void)mainButtonPressed:(id)sender; 

は、あなたの署名がどのように見えることを確認してください。

+1

...またはメソッドが存在しない可能性があります。厳密に言えば、ヘッダーファイルにメソッドを宣言する必要はありません。メソッドを見落としても、メソッドが実装されていれば、認識できないセレクターエラーは発生しません。メソッドが単純に存在しないか、またはスペルミスがあります。 – lxt

+0

メソッドが存在し、入力したとおりに正確に宣言される場合があります。 – Brodie

+0

コンパイラによって、セレクタが存在しない可能性があることを示す警告が発行される場合があります(SomeClassNameがセレクタSelectorNameに応答しないなど)。警告を避けるために、私はヘッダーにすべてのセレクターを書く方が好きです。 – Raptor

0

このView Controllerの初期化を確認してください。

+0

何を確認する必要がありますか?ここにあります:MainIconViewController * icon = [[MainIconViewController alloc] initWithNibName:nil bundle:nil]; [viewForIcons addSubview:icon.view]; icon.view.frame = CGRectMake(40、20、icon.view.frame.size.width、icon.view.frame.size.height); – Brodie

0

ビューをどのように作成していますか?VCを作成し、ビュー・プロパティーを使用してビューにアクセスするか、ビュー階層にビュー・コントローラーを表示することによって、この操作を行う必要があります。

MainIconViewController *vc = [[MainIconViewController alloc]initWithNibName:@"MainIconViewController" bundle:nil]; 
[self.view addSubview:vc.view]; 
// or.... 
[self presentModalViewController:vc animated:NO]; 

これ以外の場合、インターフェイスビルダーで行った接続はこのクラスに接続しません。

関連する問題