Search code examples
objective-cxcodeselectornsnotifications

Calling selectors from multiple file


In AppDelegate.m, I've defined

#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

I want to call alerticonstatus from allerta.h:

#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

Can I import a function whit @selector from another file like allerta.h? Now I have SIGABRT error. Can you help me? Thanks.


Solution

  • Change you method for this, it work:

    #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
    

    When you create class files, set firs letter big, like as "Allerta".