gpt4 book ai didi

ios - viewWillAppear 未执行

转载 作者:行者123 更新时间:2023-11-29 03:51:04 27 4
gpt4 key购买 nike

我有一个加载记录的表格 View ,请参阅下面的代码:

- (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];

仍然无法工作,是不是还有问题。

最佳答案

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

参见UIViewController -viewWillAppear:

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

关于ios - viewWillAppear 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17051950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com