2012-01-16 9 views
1

私のiPhoneアプリはかなりシンプルですが、エラーになります。私のアプリケーションデリゲートは、このビューコントローラを開きます。UIButtonのシンプルなアプリ - iPhoneアプリケーションのエラー

#import "Launch.h" 

@implementation Launch 

- (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]; 

    self.view.backgroundColor = [UIColor redColor]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(100, 100, 100, 100); 
    [button setTitle:@"Hello" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 
} 

-(void)buttonPressed { 
    NSLog(@"Button Pressed!"); 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo { 
    //image.image = img; 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
} 

- (void)viewDidUnload 
{ 
    [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 (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

ボタン付きの赤い画面です。ボタンをクリックすると、エラーメッセージなしでアプリがエラーになります。どうして?

ここは私のmain.mメソッドです。プログラムのエラーが出ると、 "return"行を指し、EXEC_BAD_ACCESSをスレッドに示します。

@selector(buttonPressed:) //add the :

、これを使用します:へ

#import <UIKit/UIKit.h> 

#import "testAppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([testAppDelegate class])); 
    } 
} 
+0

バックトレースには有用な情報がありません。 main.mクラスを指すだけであり、有用なエラーメッセージはありません。 – CodeGuy

+0

'buttonPressed'の' NSLog() 'にブレークポイントを設定します。 – zaph

+0

私が言ったように、エラーメッセージはありません。私のコンソールに表示される最後のことは:現在の言語:auto;現在はobjective-c (gdb) – CodeGuy

答えて

1

ターゲットクラスの割り当てが解除されているため、そのクラスを保持する必要があります。アップルのドキュメントから

:あなたは、このメソッドを呼び出すと

addTarget:action:forControlEvents:

、ターゲットは保持されません。

クラスインスタンスが保持されることを保証する必要があります。

+0

クラスは "自己"です... – CodeGuy

+0

クラスはViewControllerのようです。それはどこかでインスタンス化され、保持する必要があります。 – zaph

1

変更は

-(IBAction)buttonPressed:(id)sender;

ボタンがアクションを求めています。

+0

これはiOSでは必須ではありませんが、iOSはパラメータ、送信者のみ、または送信者とイベントの3つの形式を受け入れます。脇に:MacOSにはこの柔軟性がありません。 – zaph

+0

OH、UH私はブービーを作った、あなたは正しい、iOSではパラメータはオプションです。実際に彼のコードは動作します、それは他のものでなければなりません... –

関連する問題