Reputation: 2456
i'm new in IOS. i look for solution for over a day but all available solution work within a single UIViewConntroller but when i did it between uiTableView row selection and UIViewConntroller as Observer then the Selector is not called by Observer.
On row Selection in uitableviewcontroller
NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
NSNotification * notification =[[ NSNotification alloc] initWithName:@"NOTIFICATION" object:nil userInfo:dict];
**In UIViewController on viewdidload **
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedNotification:)
name:@"NOTIFICATION" object:nil];
**SelectorAction in uiviewcontroller **
-(void) receivedNotification:(NSNotification*) notification
{
NSLog(@"Notification Received ");
}
Upvotes: 1
Views: 999
Reputation: 2456
There is no problem in any of the above code infect i'm doing it in wrong way. I'm posing notification first and observing after posting notification. But i have to observe first and post notification after it.
I solve this problem as :
In AppDelegate didFinishLaunchingWithOptions: load viewControler first so that the observer is ready to get notification(inViewDidLoad) and after that Show/navigate to tableView And then it work perfect.This isn't the proper way but work for me. :)
Also Read out the @karthik and @Priyanka Mistry Answers. Helped me to solve the problem.
Upvotes: 1
Reputation: 621
hi @Abdul Rehman Warraich, i got your problem. you are posting notification in one view, and trying to observe it an another view. but what happens is, in the second view your observer is not ready (not loaded) to get the notification what you sent.
once you landed in the second view, that time itself observer is loaded.so it obviously will miss the notification.
so whenever you do push and pop, observer will loading as new one. so every time it will fail to observe it.
HINT: Try to load observer in your second view , before you fire the notification. Hope it will help you to debug.
Upvotes: 1
Reputation: 571
Try this i hope it would be helpful:
#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
in viewcontroller.h file
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *myData;
}
@property (weak, nonatomic) IBOutlet UITableView *mytableView;
@end
Upvotes: 0
Reputation: 131418
Stefos' answer is the usual way to post a notification, but if you do want to create a notification object manually, you then need to post it:
NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
NSNotification * notification =[[ NSNotification alloc]
initWithName:@"NOTIFICATION"
object:nil userInfo:dict];
[[NSNotificationCenter defaultCenter] postNotification: notification];
(I don't think I've ever done it in 2 steps like this. I always use one of the forms that takes a name, option, and userInfo and creates and posts the notification in one call.)
Upvotes: 0
Reputation: 464
In UITableViewController when you select the row Post Notification:
NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION" object:nil userInfo:dict];
In UIViewController viewDidLoad
method addObserver:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receivedNotification:)
name:@"NOTIFICATION" object:nil];
Write method in UIViewController:
-(void)receivedNotification:(NSNotification*) notification
{
NSLog(@"Notification Received ");
}
Check if UIViewController's instance is there in the stack? otherwise you will not get the response.
Upvotes: 1
Reputation: 167
Use tableView didSelectRowAtIndexPath Delegate:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
//Post notification here
}
Upvotes: 0
Reputation: 1235
You don't have to creat a NSNotification object. Just post the notification when row selected :
NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION" object:nil userInfo:dict];
Upvotes: 1