2012-02-13 5 views
1

をセレクタを呼び出すと、私はallerta.hから alerticonstatus を呼び出したいAppDelegate.mでは、複数のファイルから

#import "AppDelegate.h" 
#import "allerta.h" 

@implementation AppDelegate 
@synthesize window = _window; 

-(void)awakeFromNib { 

// Add an observer that will respond to loginComplete 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(alerticonstatus:) 
              name:@"alert" object:nil]; 

// Post a notification to loginComplete 
[[NSNotificationCenter defaultCenter] postNotificationName:@"alert" object:nil]; 
} 
@end 

を定義した:

#import <Foundation/Foundation.h> 
@interface allerta : NSObject{ 
} 

-(void)alerticonstatus:(NSNotification *)note; 

@end 

はallerta.m:

#import "allerta.h" 
@implementation allerta 

-(void)alerticonstatus:(NSNotification *)note { 

NSLog(@"called alerticonstatus"); 

} 
@end 

allerta.hのような別のファイルから@selectorという関数をインポートできますか? これでSIGABRTエラーが発生しました。 私を助けることができますか?ありがとう。

答えて

1

変更します。このための方法、それは動作:

#import "AppDelegate.h" 
#import "allerta.h" 

@implementation AppDelegate 
@synthesize window = _window; 

-(void)awakeFromNib { 
    allerta *_allerta = [allerta alloc]; //allocation memory 

    // Add an observer that will respond to loginComplete 
    [[NSNotificationCenter defaultCenter] addObserver:_allerta //here you called self, but you need to call your class allerta 
             selector:@selector(alerticonstatus:) 
              name:@"alert" object:nil]; 
    [_allerta release]; //kill _allerta class if you don't need more 

    // Post a notification to loginComplete 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"alert" object:nil]; 
} 
@end 

あなたが「Allerta」などのような大きなモミの手紙を設定するクラスファイルを、作成した場合。

+0

ありがとうございます。私は[_allerta release]が必要です。 OSXで? – Joannes

+0

次に、オブジェクトの割り当てメモリ(alloc、copy、new)を作成します。そのオブジェクトを必要としないよりも、オブジェクトのリリースをmakeする必要があります。 Objective-Cの開発に関する詳細情報を読む必要があります。 – alexmorhun

+0

あなたは 'allerta * _allerta = [allerta alloc];を '* _allerta = [[allerta alloc] autorelease];に変更することができますし、' [_allerta release];を書く必要はありません – alexmorhun

0

あなたの問題は、AppDelegateにこのメソッドが宣言されていない場合、alerticonstatusというメッセージの受信者としてAppDelegateを宣言していることです。あなたはこのラインでこれをやっている:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(alerticonstatus:) 
              name:@"alert" object:nil]; 

あなたのソリューションは、このような場合には、いくつかのallertaオブジェクトへのAppDelegateであるselfからオブザーバーを変更することです。ちょうどalloc-initオブジェクトallertaをオブザーバとして追加してください。

関連する問題