我的第一个视图是一个UIView控制器,我有一个UITable视图,它工作得很好。
从第一个视图中我推送另一个视图,在该视图中我选择一个组名然后视图关闭,我想要的是当我从第二个视图中选择组名称时,第一个视图将该组名的值加载到表视图中,这是我使用的代码,它不起作用
第二个视图代码
- (IBAction)sendNames:(id)sender {
if (!keyValue) {
NSLog(@"Didnt Select Any Group To Send");
return;
}
ViewController *theInstance = [[ViewController alloc] init];
[theInstance setGroupName:keyValue];
[theInstance ReceivedGroup:nil];
[self dismissViewControllerAnimated:YES completion:nil];
//tried that option and didnt work too, i added the rest of the code in the viewDidLoad
//[[NSNotificationCenter defaultCenter] postNotificationName:@"RG" object:nil];
}
keyValue 是 NSString 在 tableView didSelectRowAtIndexPath 上设置的 NSString
第一个视图
- (IBAction)ReceivedGroup:(id)sender {
NSString *path = [self dataFilePath];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *tempArray = [dict objectForKey:groupName];
nameArray = [[NSMutableArray alloc] init];
[nameArray addObjectsFromArray:tempArray];
[nameTable reloadData];
NSLog(@"Group Name : %@",groupName);
NSLog(@"Array : %@",nameArray);
}
组名是 NSString
在日志中,我打印了组名称和名称数组
但表视图是空的。
编辑:我解决了问题并发布了我的答案
您的视图控制器"theInstance"将在调用 sendNames 后立即释放:因此不会发生任何事情。您应该将第一个视图控制器的指针传递给第二个视图控制器(例如,将其设置为属性),并在此视图控制器上执行所有操作。
子类 UITableViewController
@interface SecondViewController : UITableViewController
@property (nonatomic, strong) ViewController *firstViewController;
@end
如果使用情节提要 segue 在您的第一个查看器中实现:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
((SecondViewController *) segue.destinationViewController).firstViewController = self;
}
sendNames: 应该看起来像这样:
- (IBAction)sendNames:(id)sender {
if (!keyValue) {
NSLog(@"Didnt Select Any Group To Send");
return;
}
ViewController *theInstance = self.firstViewController;
[theInstance setGroupName:keyValue];
[theInstance ReceivedGroup:nil];
[self dismissViewControllerAnimated:YES completion:nil];
//tried that option and didnt work too, i added the rest of the code in the viewDidLoad
//[[NSNotificationCenter defaultCenter] postNotificationName:@"RG" object:nil];
}
我通过使用NSNotificationCenter发送键值来修复它
这是我现在的代码
对于第二个视图
- (IBAction)sendNames:(id)sender {
if (!keyValue) {
NSLog(@"Didnt Select Any Group To Send");
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"RG" object:keyValue];
[self dismissViewControllerAnimated:YES completion:nil];
}
然后在第一视图上
- (void)receiveNotification:(NSNotification *)notification {
NSString *Key = [notification object];
nameArray = [[NSMutableArray alloc] init];
[nameArray removeAllObjects];
NSString *path = [self dataFilePath];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *tempArray = [dict objectForKey:Key];
[nameArray addObjectsFromArray:tempArray];
[self.nameTable reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[nameTable numberOfRowsInSection:0] - 1 inSection:0];
[nameTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"RG" object:nil];
}