我有一个MasterDetail应用程序和一个NavButton来添加一个新项目。我能够成功地弹回视图,但是我需要重新加载完成的表。那么我的MasterViewController究竟能够知道它何时被弹出以便重新加载?
继承Pop代码:
NavigationController.PopViewController(true);
它按预期运行,然后我需要MasterViewController重新加载表,我为它做了一个方法:
public void ReloadTable()
{
TableView.ReloadData ();
}
答案 0 :(得分:2)
试试这个:
public partial class MasterViewController : UIViewController
{
public void ReloadTable()
{
TableView.ReloadData ();
}
}
public partial class DetailViewController : UIViewController
{
public override void ViewWillDisappear (bool animated)
{
var masterViewController = NavigationController.ViewControllers.OfType<MasterViewController> ().FirstOrDefault();
if (masterViewController != null) {
masterViewController.ReloadTable ();
}
base.ViewWillDisappear (animated);
}
}
您需要添加using System.Linq;
才能获取OfType方法。
答案 1 :(得分:1)
可以通过检查navigationController是否包含这样的viewcontroller来完成:
注意 viewControllerA
推送viewControllerB
现在使用viewControllerB
使用viewWillDisappear
方法
- (void)viewWillDisappear:(BOOL)animated
{
if (![[self.navigationController viewControllers] containsObject: self]) //any other hierarchy compare if it contains self or not
{
// the view has been removed from the navigation stack or hierarchy, back is probably the cause
// this will be slow with a large stack however.
viewControllerA *objViewControllerA = [[self.navigationController viewControllers] lastObject];
//NSLog(@"%@",objViewControllerA);
if (objViewControllerA)
{
[objViewControllerA viewPoppedBack];
}
}
}
add
method
一viewControllerA
-(void)viewPoppedBack
{
//reload tableview here
[yourTableViewHere reloadData];
}