简体   繁体   English

编辑器视图控制器关闭后,UITableView不更新

[英]UITableView not updating after editor view controller dismissed

I'm a little new to iOS development. 我是iOS开发的新手。 I have these methods declared and implemented in two separate view controllers (a Master view controller and an Editor view controller) and need to pass data through from the editor to the master. 我在两个单独的视图控制器(主视图控制器和编辑器视图控制器)中声明和实现了这些方法,需要将数据从编辑器传递到主视图。 The master contains a table view that needs to update with the appropriate name. 母版包含一个表视图,需要使用适当的名称进行更新。 (Note: I used the default Master-Detail Application template for iPhone. I didn't manually create this with the Single-View Application) (注意:我使用了iPhone的默认“主从应用程序”模板。没有使用“单视图应用程序”手动创建此模板)。

Currently, the UITableView inside the MasterViewController doesn't update with a new cell. 当前,MasterViewController中的UITableView不会使用新的单元格进行更新。 It's been such a hectic week for me, I must just be missing something. 对我来说这是忙碌的一周,我一定只是想念一些东西。 Here's the code for the Master View Controller: 这是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

This is the code that I deemed necessary to see to fix the problem: obviously, I have the default overridden methods in the rest of the implementation. 这是我认为解决该问题所必需的代码:显然,在其余的实现中,我具有默认的重写方法。

This is the Editor View Controller code. 这是编辑器视图控制器代码。 When the thought appears for you, yes, the "done" button is linked to the -handleDoneButtonPushed: method in my Main Storyboard. 当想法出现时,是的,“完成”按钮链接到我的主故事板上的-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]];


}

This editor view controller simply gets some values in the form of a UITextField and hands them off to the Master View Controller through an object of that class. 该编辑器视图控制器仅以UITextField的形式获取一些值,然后通过该类的对象将其交给主视图控制器。 Again, when I click the Done button on my top nav-bar, it dismisses the Editor View Controller but doesn't update the table view with those values retrieved. 同样,当我单击顶部导航栏上的“完成”按钮时,它会关闭“编辑器视图控制器”,但不会使用检索到的值更新表视图。

Help would be greatly appreciated. 帮助将不胜感激。 :) :)

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

This is wrong, you already has an instance of MasterViewController you should not instantiate a new one, pass the data and expect the previous one to update the detailed value. 这是错误的,您已经有一个MasterViewController实例,您不应实例化一个新实例,传递数据并期望上一个实例更新详细值。

You should use the delegation pattern. 您应该使用委托模式。

Create a Protocol in FTEditorController . FTEditorController创建一个协议。 A detailed post on implementing Delegates in Objective-C 有关在Objective-C中实现代表的详细信息

@protocol FTEditorControllerDelegate <NSObject>

@optional
- (void)editorController:(FTEditorViewController *)editorController didCompleteEditingTrinomial:(NSString *)trinomial;

@end

When you set MasterController as delegate of EditorController, once an event of interest occurs in EditorController, it gives that event to its delegate. 当您将MasterController设置为EditorController的委托时,一旦在EditorController中发生了感兴趣的事件,它将将该事件提供给其委托。

So once you are done with creating trinomial expression pass it to MasterController. 因此,一旦创建完三项式表达式,便将其传递给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.

相关问题 在发送方视图控制器被解除后执行 segue - Performing segue after sender view controller is dismissed 关闭UIPopoverController后呈现视图控制器 - Presenting a View Controller after a UIPopoverController is dismissed 关闭另一个视图控制器后,停止重新加载视图控制器 - stop view controller from reloading after another view controller is dismissed 父视图控制器未关闭 - Parent view controller not dismissed 为什么在取消模态视图控制器后从视图中删除CAGradientLayer - Why is the CAGradientLayer removed from view after modal view controller is dismissed 在iOS上关闭View Controller后,如何继续播放音频? - How to continue to play audio after view controller is dismissed on iOS? 视图控制器被解雇后如何显示警报 - How to present an alert after view controller has been dismissed iOS视图控制器内存在被解除后未释放 - iOS view controller memory not released after it's been dismissed iOS /自动版式:取消已演示的控制器后,更改为演示视图 - IOS/Autolayout: Change to Presenting View After Presented Controller Dismissed 视图通过导航控制器解散后,应用程序崩溃了吗? - The app crashes after view is dismissed through navigation controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM