2013-03-04 68 views
0

viewController我有一個modalViewController通過tableView出現在viewController的頂部。當用戶點擊modalViewController一個按鈕,我想用這個的viewController內重裝的tableView:調用[tableView reloadData];從一個modalViewController

[tableView1 reloadData]; 

我不希望把在viewDidAppear或viewWillAppear中方法的重載,因爲他們被調用的時候,不需要tableView重新加載(即當用戶點擊後退按鈕返回到tableView)。

有沒有辦法做到這一點?

+0

通過通知或委託。 – 2013-03-04 05:32:23

+0

嗨Anil,我不完全確定如何做到這一點。你有任何例子或建議入門? – Brandon 2013-03-04 05:33:26

+2

@Brandon去參加代表。這對你真的很有幫助。 – 2013-03-04 05:41:48

回答

0

使用通知狀波紋管方法: -

在yourViewController的ViewdidLoad Mehod

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(ReloadDataFunction:) 
                name:@"refresh" 
                object:nil]; 

    [super viewDidLoad]; 

} 
-(void)ReloadDataFunction:(NSNotification *)notification { 

    [yourTableView reloadData]; 

} 

立即創建NSNotificationCenter你可以從你modelViewController後退按鈕調用此通知,否則你調用這個刷新通知希望像把這行代碼: -

[[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:self]; 

注:postNotificationName:@"refresh"這是特別通知

2

嘗試

1)寫其reloadstable data一種方法。

2)在後面打電話button點擊。

+0

嗨吉瑞什,我建立了方法。我想在用戶點擊模式視圖上的按鈕時調用它,而不是後退按鈕。你知道如何去做這件事嗎? – Brandon 2013-03-04 05:34:32

+0

我相信Girish意味着在viewcontroller(它有你的tableview)內創建你的重載方法,並從modalviewcontroller中調用它 – 2013-03-04 05:36:39

+0

在這種情況下,創建一個委託,當你點擊按鈕時自動調用。 – Girish 2013-03-04 05:36:39

0

嘗試使用這一

讓一個按鈕,點擊這個按鈕,比你可以重新載入數據。 此按鈕用於自定義並在後臺使用它。

- (IBAction)reloadData:(id)sender 
    { 
     [tblView reloadData]; 
    } 
1

您可以使用委託的一個關鍵。如果發現它更難,那麼替代方案是使用NSNotificationCenter。您可以看到Refreshing TableView的可接受答案。這真是非常簡短,容易理解的方式。

2

這是典型的委託模式的問題,在你的模式視圖控制器,你需要一個委託參照當前視圖控制器呈現它現在

//Modal 
@protocol ModalVCDelegate 
- (void)tappedBackButton; 
@end 

@class ModalVC: UIViewController 
@property id<ModalVCDelegate> delegate; 
@end 

@implementation 
- (void)backButtonTapped:(id)sender 
{ 
    if (self.delegate) 
     [self.delegate tappedBackButton]; 
} 
@end 

,在您的介紹VC,只是處理該委託消息

//Parent VC 
- (void)showModal 
{ 
    ModalVC *vc = [ModalVC new]; 
    vc.delegate = self; 
    //push 
} 

- (void)tappedBackButton 
{ 
    [self.tableView reloadData]; 
    //close modal 
} 
-2
You can use NSNotification to refresh table on ViewController. 

Inside viewController : 

-(void)dealloc{ 
[[NSNotificationCenter defaultCenter] removeObserver:self]; 
[super dealloc]; 
} 

Write code in viewDidLoad: 
[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(reloadMainTable:) 
     name:@"ReloadTable" 
     object:nil]; 




- (void) reloadMainTable:(NSNotification *) notification 
{ 
    [tableView reload]; 
} 


Inside ModelViewController: 
[[NSNotificationCenter defaultCenter] 
     postNotificationName:@"ReloadTable" 
     object:nil]; 

Here you can also send custom object instead of nil parameter. But be care full about removal of NSNotification observer. 
+0

任何人都可以告訴我爲什麼這個答案讓人投票嗎? – 2013-03-04 06:06:18

+0

你好,任何人都可以告訴我嗎? – 2013-03-04 06:14:36

相關問題