我有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 :(得分: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
发布通知的viewcontroller
@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
答案 1 :(得分: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];
}