My First View是一个UIViewController,我有一个UITable View,它运行得很好。
从第一个视图我推另一个视图,在该视图中我选择一个组名然后视图关闭,我想要的是当我从第二个视图中选择组名时第一个视图加载该组名的值到表视图,这是我使用的代码,它不起作用
第二个视图代码
- (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 是在 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);
}
groupName 是NSString
在日志中,我获得 groupName 并打印 nameArray
但tableView为空。
编辑:我修复了问题并发布了答案
答案 0 :(得分:0)
你的viewcontroller“theInstance”将在调用sendNames后立即解除分配:所以什么都不会发生。您应该将第一个viewcontroller的指针传递给第二个viewcontroller(例如将其设置为属性)并对此viewcontroller执行所有操作。
子类UITableViewController
@interface SecondViewController : UITableViewController
@property (nonatomic, strong) ViewController *firstViewController;
@end
如果在第一个viewcontroller中使用storyboard segues实现:
- (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];
}
答案 1 :(得分:0)
我通过使用 NSNotificationCenter
发送 keyValue 来解决此问题这是我现在的代码
第二种观点
- (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];
}