NSNotification不适用于ios 9.0 xcode 7.3.1

时间:2016-05-19 06:47:07

标签: objective-c nsnotificationcenter nsnotifications

// Post method
 NSDictionary *dict = [NSDictionary dictionaryWithObject:@"india" forKey:@"Country"];
 [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_1" object: dict userInfo:nil];


//reciever method
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"NOTIFICATION_1" object:nil];
- (void)receivedNotification: (NSNotification*) notification
{


        NSLog(@"deepak kumar =%@",notification);

}

1 个答案:

答案 0 :(得分:0)

试试这个我希望它对你有所帮助!!!

在viewcontroller.m文件中

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myData = [[NSMutableArray alloc]init];
    myData = [[NSMutableArray alloc]initWithObjects:@"india",@"Japan",@"pakistan",@"srilanka", nil];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:)name:@"NOTIFICATION" object:nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *const identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    cell.textLabel.text = [myData objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION" object:nil userInfo:dict];
}


-(void) receivedNotification:(NSNotification*) notification {
    NSLog(@"Notification  Received ");
}

@end
在viewcontroller.h文件中

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{

    NSMutableArray *myData;
}

@property (weak, nonatomic) IBOutlet UITableView *mytableView;

@end