2013-03-09 98 views
0

我對iOS開發有點新。我在兩個獨立的視圖控制器(一個主視圖控制器和一個編輯器視圖控制器)中聲明並實現了這些方法,並且需要將數據從編輯器傳遞到主控。主文件包含需要用適當名稱更新的表格視圖。 (注:我使用iPhone的默認主從應用程序模板我沒有手動與單查看應用程序創建此。)編輯器視圖控制器解僱後UITableView不更新

目前,MasterViewController內的UITableView不以新的細胞更新。對我來說這是一個忙碌的一週,我必須錯過一些東西。下面是主視圖控制器代碼:

@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的形式,一些值和手趕走到主視圖控制器通過這個類的一個對象。同樣,當我點擊頂部導航欄上的「完成」按鈕時,它將取消編輯器視圖控制器,但不會使用檢索到的值更新表格視圖。

幫助將不勝感激。 :)

回答

0
-(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中創建協議。在實施Delegates in 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]; 

} 
+0

A-hah!我知道我有些失蹤。之前我曾使用過這種授權模式,但我在如何使用該方法時正在考慮錯誤的方式。讓我試試看看它是如何工作的。 – 2013-03-09 19:17:42

+0

@HunterE。很高興幫助你。三項式的插入是你需要處理的東西。 – Anupdas 2013-03-09 19:22:37

+0

那麼,在-handleDoneButtonPushed:方法下,我這樣做了:[self.delegate editorController:self didCompleteEditingTrinomial:trinomialString]然後通過協議並在FTMasterViewController中實現該方法。我只是將代碼直接粘貼到實現中,但表視圖仍未更新。我修改了-tableView:cellForRowAtIndexPath:來處理數組,但仍然沒有。思考? – 2013-03-09 19:37:06

相關問題