2013-09-30 151 views
1

我有一個UITableView嵌入UIView,我無法弄清楚如何更新。當我在UIView中嵌入UITableView來到這個頁面時,有一個按鈕,當按下時會出現一個modalForm。有一個用戶輸入名稱的textField,然後按下另一個按鈕來「創建」該對象,關閉模式窗體並更新應用程序數據。但是,我不知道如何刷新我的UITableView數據...有沒有辦法告訴UITableView刷新modalForm視圖時被解僱?更新表查看數據

編輯:我知道我需要發送此消息[tableView reloadData];爲了刷新,但我想知道我可以把它放在哪裏,所以它被稱爲解除modalForm?

+0

[' - [UITableView reloadData]'](https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/reloadData)?或KVC,但這是一個更高級的話題。 – Kevin

+0

你還在爲此工作嗎?想辦法? –

+0

還沒有。我暫時解決了這個問題,只需在導航堆棧上推視圖控制器,而不是進行模態轉換,但如果可能的話,我仍然希望能夠進行模態演示...... – ngoue

回答

1

在顯示和解除模態的視圖控制器中,您應該調用dismissViewControllerAnimated:completion方法來解除模態。模態不應該自行解散。你可以使用一個完成塊來執行你想要的任何代碼,當模態完成解除時它將執行。下面的例子。

[self dismissViewControllerAnimated:YES completion:^{ 
    //this code here will execute when modal is done being dismissed 
    [_tableView reloadData]; 
}]; 

當然,不要忘了避免在塊中強烈捕獲自己。

如果您最終得到模態消除本身,您將需要一個委託方法,以便模態可以返回到呈現視圖控制器或由模式發送並由呈現視圖控制器捕獲的通知,或者您可以在呈現視圖控制器中實現viewWillAppear:。每當視圖即將出現時,此方法都會觸發。這意味着第一次以及在一個模式被解僱之後,並且即將展示它正在呈現的視圖。

--------------------------------------------- -------------------------------------------

以下是一個寫你自己的協議和使用它的例子。

MyModalViewController.h在任何你想在你的MyModalViewController實現文件

@protocol MyModalViewControllerDelegate <NSObject> 

//if you don't need to send any data 
- (void)myModalDidFinishDismissing; 

//if you need to send data 
- (void)myModalDidFinishDismissingWithData:(YourType *)yourData 

@end 

@interface MyModalViewController : UIViewController 

@property (weak) id <MyModalViewControllerDelegate> delegate; 

//place the rest of your properties and public methods here 

@end 

的,叫你選擇的委託方法。你應該首先確保你的委託實際上對選擇器做出了響應。 MyModalViewController.m if([self.delegate respondsToSelector:@selector(myModalDidFinishDismissing)]) [self.delegate myModalDidFinishDismissing];

在呈現模態的視圖控制器中,您需要在頭文件中聲明符合協議,您需要將模態的委託設置爲viewcontroller,並確保實際實現了委託方法你打算使用。

MyPresentingViewController.h

@interface MyPresentingViewController : UIViewController <MyModalViewControllerDelegate> 

MyPresentingViewController.m

myModal.delegate = self; 
- (void)myModalDidFinishDismissing { 
    //do something 
    [tableView reloadData]; 
} 
+0

呈現視圖控制器應採用哪種協議來委派? – ngoue

+0

而且,閱讀文檔時,我的印象是viewWillAppear在從presentationViewController返回時不會調用?或者至少不是模態的? – ngoue

+1

如果您要在呈現vc中採用委託,則需要在您創建的vc中創建該協議。我不確定viewWillAppear片段,所以我在回答之前在本地測試了它。在viewWillAppear中放置一個斷點,當我解除一個呈現的(模態)vc時,它會跳閘。 –

0

試試這個行:

[self.tableView reloadData]; 
+0

我想知道如何在我解僱modalForm後調用該行?有沒有一種方法,當我解僱modalForm被調用? – ngoue

0

你可以嘗試把[tableView reloadData]在viewWillAppear中的方法在您的tableView是。

1

你可以在模態表單類中擁有一個委託變量。當你關閉你的模態表單時,你可以調用類似[delegate performSelector:@selector(modalFormClosed)]的方法,它調用[tableView reloadData]。

@interface ModalForm : UIViewController 
@property (nonatomic, weak) id delegate; 
@end 

@implementation ModalForm 
- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [self.delegate performSelector:@selector(modalFormClosed)]; 
} 
@end 

在你的類使用了MODALFORM:

myModalForm.delegate = self; 

並確保您有modalFormClosed方法太:

- (void)modalFormClosed { 
    [self.tableView reloadData]; 
} 

,也可以送一個NSNotification(考慮NSNotificationCenter)當你的模態形式消失。

+0

當我嘗試實現這一點時,遇到了無法「識別」呈現視圖控制器的問題。實際上,當我NSLog我的modalForm的presentationViewController的值,它記錄NULL。有什麼建議麼? – ngoue

+0

您可能想要在問題中粘貼代碼。你提交的vc肯定知道誰在介紹它。 –

1

我希望我有這個參考,但幾年前我看到了蘋果的一份說明,表示一種模態視圖不應該忽視自己。相反,您可以在呈現視圖控制器上調用方法。在這種方法中,演示者關閉模態視圖控制器。通常我把一個類別上我UIViewControllers:

@interface UIViewController (Extension) 
-(void)simpleDismissAnimated:(BOOL)animated; 
@end 
@implementation UIViewController (Extension) 
-(void)simpleDismissAnimated:(BOOL)animated { 
[self dismissViewControllerAnimated:animated completion:nil]; 
} 

將它包含在所有的視圖控制器子類,或在您的PCH文件。

因此,有你的模式視圖控制器電話:

[self.presentingViewController simpleDismissAnimated:YES]; 

然後在您呈現視圖控制器,覆蓋simpleDismissAnimated執行:喜歡的東西:

-(void)simpleDismissAnimated:(BOOL)animated { 
[_tableView reloadData]; 
[self dismissViewControllerAnimated:animated completion:nil; 
} 

這應該照顧它。

0

我有同樣的問題。 ReloadData不起作用。我解決了它,這樣我犯了這樣的新方法:

- (void)insertNewString:(NSString *)text{ 
     if (!_yourArrayWithData) { 
      _yourArrayWithData = [[NSMutableArray alloc] init]; 
     } 
     [_yourArrayWithData insertObject:text atIndex:0]; 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
     [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 

,當我想添加一些的tableView,我用的按鈕,操作是這樣的:

- (IBAction)SaveNewPhrase:(id)sender {   
NSString *mainText = _NewPhraseTextView.text; //I take nsstring from textView 

if([mainText isEqual:@""]){ 
    return; 
}else{ 
    NSMutableArray *contentFile = [NSArray arrayWithContentsOfFile:docPathContents()]; 
    NSMutableArray *copyContentFile = [[NSMutableArray alloc] init]; 

      //save array with new item   
    [copyContentFile addObject:mainText]; 
    [copyContentFile addObjectsFromArray:contentFile]; 
    [copyContentFile writeToFile:docPathContents() atomically:YES]; 

    [self insertNewString:mainText];  //refresh tableView 

    [_NewPhraseTextView resignFirstResponder]; 
}} 

我希望,它有助於。