-1

My First View is a UIViewController i have a UITable View in it and it works just fine.

From the first view i push another view, in that view i select a group name then the view dismiss, what i want is when i select the group name from the second view the first view loads the values of that group name to the table view, this is the code i use and its not working

the second view code

- (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 is NSString that been set on tableView didSelectRowAtIndexPath

the first view

- (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 is NSString

In the log i get the groupName and the nameArray printed

but the tableView is empty.

EDIT: I fixed the problem and posted my answer

4

2 回答 2

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];
}
于 2013-09-09T08:39:13.900 回答
0

您的视图控制器“theInstance”将在调用 sendNames 后立即被释放:所以什么都不会发生。您应该将第一个视图控制器的指针传递给您的第二个视图控制器(例如将其设置为属性)并在此视图控制器上执行所有操作。

子类 UITableViewController

@interface SecondViewController : UITableViewController
@property (nonatomic, strong) ViewController *firstViewController;
@end

如果在您的第​​一个视图控制器中使用故事板 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];
}
于 2013-09-08T12:26:04.630 回答