viewWillAppear未执行

时间:2013-06-11 19:14:01

标签: ios xcode

我有一个用记录加载的tableview,请参阅下面的代码:

- (void)viewDidLoad
{
    self.navigationItem.title=@"Accounts";
//    NSString *accountFile = [[NSBundle mainBundle] pathForResource:@"Accounts2" ofType:@"plist"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentPath stringByAppendingPathComponent:@"Accounts2.plist"];
    accounts = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"Accounts contains : %@", accounts);
    account = [accounts objectForKey:@"Account"];
    NSLog(@"account %@", account);
    number = [accounts objectForKey:@"Number"];
    dueDay = [accounts objectForKey:@"DayDue"];
    minAmount = [accounts objectForKey:@"MinAmount"];
    balance = [accounts objectForKey:@"Balance"];

    NSLog(@"data loaded"); 

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
//    if (!_objects) {
//        _objects = [[NSMutableArray alloc] init];
 //   }
 //   [_objects insertObject:[NSDate date] atIndex:0];
 //   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 //   [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (!self.addView) {
        self.addView = [[AddView alloc] initWithNibName:@"AddView" bundle:nil];
    }
          [self.navigationController pushViewController:self.addView animated:YES];
    NSLog(@"I am done, now what");
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Number of records is %d", [account count]);
    return [account count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


     NSDate *object = _objects[indexPath.row];
     cell.textLabel.text = [object description];

    NSString *nameOfAccount = [account objectAtIndex:indexPath.row];
    cell.textLabel.text = nameOfAccount;
    NSString *accountNumber = [number objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = accountNumber;
    NSLog(@"Index %d", indexPath.row);
    return cell;
}

我想在表中添加一条记录,所以我使用“InsertNewObject”方法...出现新屏幕,我可以将记录添加到我的plist中,然后在DetailView中执行以下行返回:

[self.navigationController popToRootViewControllerAnimated:NO];

现在,我想用我的新记录重新加载我的TableView: 我以为我必须有以下viewWillAppear方法,如下所示:

- (void) viewWillAppear {
    NSLog(@"View will appear");
    [super viewWillAppear:YES];
    [self.tableView reloadData];

}

但是,我的NSLog永远不会被执行,因此我的程序永远不会到达这里......我做错了什么,谢谢你的回复

我添加了以下两种方法:

- (void) navigationController:(UINavigationController *) navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    [self.tableView reloadData];

}

-(void) navigationController:(UINavigationController *) navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self.tableView reloadData];
}

并更改了行

[self.navigationController popToRootViewControllerAnimated:YES];

仍然无法正常工作,还有一些问题。

3 个答案:

答案 0 :(得分:2)

您没有使用正确的方法。您要遗漏animated参数,请尝试使用 -(void)viewWillAppear:(BOOL)animated

请参阅UIViewController - viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"View will appear");
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}

答案 1 :(得分:0)

如果您使用的是导航控制器我相信重新加载视图的正确方法是调用像这样的somethihng

  -(void) viewWillAppear: (BOOL) animated 
 { 
   [self.tableView reloadData];
 }

您还可以创建自己的自定义reloadData方法,设置您想要更改的内容,然后调用该方法。

答案 2 :(得分:-1)

正如所指出的,viewWillAppear采用animated参数。但是,即使添加它,从导航堆栈弹出视图控制器也不会触发对viewWillAppear:的调用。您需要使用UINavigationControllerDelegate的{​​{1}}和navigationController:willShowViewController:animated:方法重新加载表格视图。