2015-11-06 14 views
0

我有2个视图控制器-1用于输入来自用户的值,另一个用于在表格视图中显示值。编码去如下:何传递值使用通知?

这是用于显示由所述通知传递的值:

ListTableViewContrller.m

@property(strong,nonatomic)NSMutableArray *namearray; 
@end 

@implementation ListTableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receieveTestNotificatiom:) name:@"TestNotification" object:nil]; 


} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(void)receieveTestNotificatiom:(NSNotification *)notification 
{ 
    _namearray=[[NSMutableArray alloc]initWithObjects:[notification.userInfo objectForKey:@"USERNAME"], nil]; 
    [self.tableView reloadData]; 
} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return _namearray.count; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath]; 


    cell.detailTextLabel.text=[_namearray objectAtIndex:indexPath.row]; 

    return cell; 
} 

这对于在命名为视图控制器从所述用户输入数据source.m -

- (IBAction)senddata:(id)sender 
{ 
    NSDictionary *dict=[NSDictionary dictionaryWithObject:_nametextfield.text forKey:@"USERNAME"]; 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:nil userInfo:dict]; 
} 

当我尝试运行我的应用程序,它不显示在表视图控制器输入的值。为什么这样?在此先感谢......

+0

什么r esult你得到在Namearray –

+0

我得到在文本字段中输入的值。 –

+0

你可以显示/打印你的名字阵列,你的编码是好​​的,但它错过了索引 –

回答

0

不喜欢和尝试这个

- (void)viewDidLoad { 
[super viewDidLoad]; 
// allocate the memory here 
_namearray=[NSMutableArray new]; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receieveTestNotificatiom:) name:@"TestNotification" object:nil]; 


} 

-(void)receieveTestNotificatiom:(NSNotification *)notification 
{ 



    NSString *value = [NSString stringwithFormat:@"%@",[notification.userInfo objectForKey:@"USERNAME"]]; 

    NSLog(@"%@ value", [notification userInfo]); 
    [_namearray addobject:value]; 
[self.tableView reloadData]; 
} 
+0

告诉我你在这里得到的结果NSLog(@“%@ value”,[notification userInfo]); –

+0

o/p显示我在文本字段中输入的文本...但它不在表格中显示 –

+0

是的,先生它正在工作,但只要我输入第二个名称,它就会擦除表中的第一个名称。为什么这样? –

0

您的代码看起来不错。但是您是否真的想在每次收到通知时重新实例化阵列?或者你想实例化阵列只有一次,然后添加值,你通过通知?如果最后是你想要的这里是我的解决方案:

接收tableviewcontroller

@interface TableViewController() 

@property (strong, nonatomic) NSMutableArray *names; 

@end 

@implementation TableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.estimatedRowHeight = 44.0; 
    self.tableView.rowHeight = UITableViewAutomaticDimension; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"NEW_NAME_NOTIFICATION" object:nil]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.names.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NameCell" forIndexPath:indexPath]; 
    cell.textLabel.text = self.names[indexPath.row]; 
    return cell; 
} 

- (void)receivedNotification:(NSNotification *)notification { 
    [self.names addObject:notification.userInfo[@"value"]]; 
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.names.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

- (NSMutableArray *)names { 
    if (!_names) { 
    _names = [NSMutableArray array]; 
    } 
    return _names; 
} 

@end 

视图 - 控制该帖子通知

@interface NewNameViewController() 

@property (weak, nonatomic) IBOutlet UITextField *textField; 

@end 

@implementation NewNameViewController 

- (IBAction)doneBarButtonItemTapped:(UIBarButtonItem *)sender { 
    [self.presentingViewController dismissViewControllerAnimated:YES completion:^{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NEW_NAME_NOTIFICATION" object:nil userInfo:@{@"value": self.textField.text}]; 
    }]; 
} 

@end 
+0

先生你没有说我在哪里编码问题? –

+0

我说“你的代码看起来不错”。我看不到任何错误。也许问题是你没有发布的代码的其他部分?!你想分享一些,所以我可以帮忙吗? –