调用[tableView reloadData];从一个modalViewController

问题描述:

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

使用通知状波纹管方法: -

在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"这是特别通知

尝试

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

尝试使用这一

让一个按钮,点击这个按钮,比你可以重新载入数据。 此按钮用于自定义并在后台使用它。

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

您可以使用委托的一个关键。如果发现它更难,那么替代方案是使用NSNotificationCenter。您可以看到Refreshing TableView的可接受答案。这真是非常简短,容易理解的方式。

这是典型的委托模式的问题,在你的模式视图控制器,你需要一个委托参照当前视图控制器呈现它现在

//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 
} 

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