gpt4 book ai didi

ios - 未从其他 View 填充列表

转载 作者:行者123 更新时间:2023-11-29 12:39:59 26 4
gpt4 key购买 nike

我有一个 View Controller ,用户可以在其中填充项目。退出 View 时,应保存该对象,并由包含所有这些对象列表的先前 View 加载。我的 NSLog 显示该对象正在保存,但我无法让它出现在 ListView 中。

两个 Controller 的代码如下。谢谢

列表 Controller

#import "ItemsViewController.h"
#import "Calculation.h"
#import "CalculationItemStore.h"
#import "CalculationDetailViewController.h"

@interface ItemsViewController()
@property UISegmentedControl *segment;

@end

@implementation ItemsViewController

- (instancetype)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {

UINavigationItem *navItem = self.navigationItem;
navItem.title = @"MACS";

// Create a new bar button item that will send
// addNewItem: to CalculationsViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];


// Set this bar button item as the right item in the navigationItem
navItem.rightBarButtonItem = bbi;

navItem.leftBarButtonItem = self.editButtonItem;

}
return self;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

- (void)viewDidLoad
{
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Publication", @"About", nil]];
self.tableView.tableHeaderView = _segment;
[_segment addTarget:self action:@selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];


[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"UITableViewCell"];
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[CalculationItemStore sharedStore] allCalculations] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get a new or recycled cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];

cell.textLabel.text = item.title;

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc] init];

NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *selectedItem = items[indexPath.row];

// Give detail view controller a pointer to the item object in row
detailViewController.calculation = selectedItem;

// Push it onto the top of the navigation controller's stack
[self.navigationController pushViewController:detailViewController
animated:YES];
}

- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[CalculationItemStore sharedStore] allCalculations];
Calculation *item = items[indexPath.row];
[[CalculationItemStore sharedStore] removeItem:item];

// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}

- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[CalculationItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row
toIndex:destinationIndexPath.row];
}

- (void)segmentPressed:(id)sender {

if (_segment.selectedSegmentIndex ==0) {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://ferm.forestry.oregonstate.edu/facstaff/leshchinsky-ben"]];

}else if(_segment.selectedSegmentIndex ==1){

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:@"MACSLoad@2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;

}


}

- (IBAction)addNewItem:(id)sender
{
// Create a new Calculation and add it to the store
Calculation *newItem = [[CalculationItemStore sharedStore] createCalculation];

CalculationDetailViewController *detailViewController = [[CalculationDetailViewController alloc]initForNewItem:YES];
detailViewController.calculation = newItem;

detailViewController.dismissBlock = ^{
[self.tableView reloadData];
};

UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:detailViewController];
navController.modalPresentationStyle = UIModalPresentationFullScreen;
// [self presentViewController:navController animated:YES completion:NULL];
[self.navigationController pushViewController:detailViewController animated:YES];
}

@end

元素 Controller 保存方法

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];

//clear responder
[self.view endEditing:YES];

//save changes
BOOL success = [[CalculationItemStore sharedStore]saveChanges];
if(success){
NSLog(@"Saved all calcs");

}else{
NSLog(@"failure saving");
}
}

最佳答案

我认为问题是在第一个 Controller 上调用表 reloadData 时,第二个 Controller 尚未完成加载。一旦第二个 VC 完成保存,您可以通知第一个 VC 重新加载数据可以使用

通知
  1. 在第二个或

  2. 上创建一个委托(delegate)
  3. 使用通知

如果您需要有关如何执行此操作的更多详细信息,请告诉我!

看起来对象创建也有问题。您的计算元素未正确创建。当您准备好保存时...查看 self.privateItems 的内容。他们填写的值为零。您将需要正确实例化字符串。

engineerName 和其他字符串也需要分配。当用户点击完成时,文本框中的值需要设置为您的数据模型。

关于ios - 未从其他 View 填充列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234728/

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