[英]UITableView not updating after editor view controller dismissed
我是iOS开发的新手。 我在两个单独的视图控制器(主视图控制器和编辑器视图控制器)中声明和实现了这些方法,需要将数据从编辑器传递到主视图。 母版包含一个表视图,需要使用适当的名称进行更新。 (注意:我使用了iPhone的默认“主从应用程序”模板。没有使用“单视图应用程序”手动创建此模板)。
当前,MasterViewController中的UITableView不会使用新的单元格进行更新。 对我来说这是忙碌的一周,我一定只是想念一些东西。 这是Master View Controller的代码:
@implementation FTMasterViewController
@synthesize bPlusMinusField, cPlusMinusField, cTerm, bTerm;
-(void) passTrinomial:(CCTrinomial *)tri withBPlusMinusField:(NSString *)bField withBTerm:(double)bTermField withCPlusMinusField:(NSString *)cField withCTerm:(double)cTermField {
//allocate the array
if (!self.trinomials) {
self.trinomials = [[NSMutableArray alloc] init];
}
//set Master VC properties to tri fields passed in
self.bPlusMinusField = bField;
self.cPlusMinusField = cField;
self.bTerm = bTermField;
self.cTerm = cTermField;
NSString *trinomial = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField, bTerm, cPlusMinusField, cTerm];
[self.trinomials insertObject:trinomial atIndex:0];
NSLog(@"%lu", (unsigned long)[self.trinomials count]);
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.trinomials count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField, bTerm, cPlusMinusField, cTerm];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
@end
这是我认为解决该问题所必需的代码:显然,在其余的实现中,我具有默认的重写方法。
这是编辑器视图控制器代码。 当想法出现时,是的,“完成”按钮链接到我的主故事板上的-handleDoneButtonPushed:方法。
-(IBAction)handleDoneButtonPushed:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
FTMasterViewController *masterVC = [[FTMasterViewController alloc] init];
[masterVC passTrinomial:factoring withBPlusMinusField:bPlusMinusField.text withBTerm:[bTriField.text doubleValue] withCPlusMinusField:cPlusMinusField.text withCTerm:[cTriField.text doubleValue]];
}
该编辑器视图控制器仅以UITextField的形式获取一些值,然后通过该类的对象将其交给主视图控制器。 同样,当我单击顶部导航栏上的“完成”按钮时,它会关闭“编辑器视图控制器”,但不会使用检索到的值更新表视图。
帮助将不胜感激。 :)
-(IBAction)handleDoneButtonPushed:(id)sender { [self dismissViewControllerAnimated:YES completion:NULL]; FTMasterViewController *masterVC = [[FTMasterViewController alloc] init]; [masterVC passTrinomial:factoring withBPlusMinusField:bPlusMinusField.text withBTerm:[bTriField.text doubleValue] withCPlusMinusField:cPlusMinusField.text withCTerm:[cTriField.text doubleValue]]; }
这是错误的,您已经有一个MasterViewController实例,您不应实例化一个新实例,传递数据并期望上一个实例更新详细值。
您应该使用委托模式。
在FTEditorController
创建一个协议。 有关在Objective-C中实现代表的详细信息
@protocol FTEditorControllerDelegate <NSObject>
@optional
- (void)editorController:(FTEditorViewController *)editorController didCompleteEditingTrinomial:(NSString *)trinomial;
@end
当您将MasterController设置为EditorController的委托时,一旦在EditorController中发生了感兴趣的事件,它将将该事件提供给其委托。
因此,一旦创建完三项式表达式,便将其传递给MasterController。
-(IBAction)handleDoneButtonPushed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *trinomial = [NSString stringWithFormat:@"x² %@ %g %@ %g", bPlusMinusField.text, [bTriField.text doubleValue], cPlusMinusField.text, [cTriField.text doubleValue]];
if([self.delegate respondsToSelector:@selector(editorController:didCompleteEditingTrinomial:)])
{
[self.delegate editorController:self didCompleteEditingTrinomial:trinomial];
}
}
//FTEditorControllerDelegate implementation in MasterViewController
- (void)editorController:(FTEditorViewController *)editorController didCompleteEditingTrinomial:(NSString *)trinomial{
//allocate the array
if (!self.trinomials) {
self.trinomials = [[NSMutableArray alloc] init];
}
//This condition is tricky, I assume you always want to display the edited
//trinomial expression as the first one in tableView
[self.trinomials insertObject:trinomial atIndex:0];
//Reloads the tableView to reflect the changes
[self.tableView reloadData];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.