我有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];
}
当我尝试运行我的应用程序,它不显示在表视图控制器输入的值。为什么这样?在此先感谢......
什么r esult你得到在Namearray –
我得到在文本字段中输入的值。 –
你可以显示/打印你的名字阵列,你的编码是好的,但它错过了索引 –