Xcode 如何在MasterViewController中更新表格

Xcode 如何在MasterViewController中更新表格,xcode,ipad,Xcode,Ipad,我使用的是主详细信息模板。 我在细节视图中有一个分段控件,并且我已将MasterViewController设置为代理。 这使我能够为用户提供选择。 我知道分段控件正在工作,并将选择传递给MVC 我希望每个选项都触发一组新的数据,然后可以将这些数据加载到MasterViewController的表视图中。 我的问题是无法找到更新表视图中数据的方法。您可以使用MasterViewController中声明的实例变量在UISegmentedControl更改时进行更新。基于此,当您返回到主视图控制器

我使用的是主详细信息模板。 我在细节视图中有一个分段控件,并且我已将MasterViewController设置为代理。 这使我能够为用户提供选择。 我知道分段控件正在工作,并将选择传递给MVC

我希望每个选项都触发一组新的数据,然后可以将这些数据加载到MasterViewController的表视图中。
我的问题是无法找到更新表视图中数据的方法。

您可以使用
MasterViewController
中声明的实例变量在
UISegmentedControl
更改时进行更新。基于此,当您返回到主视图控制器时;使用其
视图将出现
[tableView reloadData]

如果我误解了你的问题,请告诉我

编辑:

取一个
整数
将其命名为
分段索引
;当段发生更改时更新。根据
segmentIndex
的值,加载需要显示在
masterview控制器的
UITableView
上的
dataSource

编辑2:

UISegmentedControl
的值更改时,将
mvc.segmentIndex=(您的segmentedcontrol的当前值)
; 然后在
MasterViewController
视图中将出现

switch (self.segmentIndex)
{
    case 0:
           // Set Datasource for First Choice.
           break;
    case 0:
           // Set Datasource for Second Choice.
           break;
    case 0:
           // Set Datasource for Third Choice.... and so on...
           break;
    default:
           // Default Behavior
           break;
}

希望你明白我的意思。

如果是我,我会使用通知而不是代理。i、 e使用detailViewController内部的valueChanged检测分段控制值更改,然后发布来自detailViewController的NSNotification,masterViewController将收到该通知

最简单的方法是:

[[NSNotificationCenter defaultCenter] postNotificationName:@"segmentOneChosen" object:nil userInfo:nil];
您是否已注册此通知的masterViewController:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(segmentOneChosen) name:@"segmentOneChosen" object:nil];
最好还是将选择的值与通知一起传递,这样就不需要为每个段单独发送通知

!!未测试代码:

NSArray *keys = [NSArray arrayWithObjects:@"segmentChosen", nil];
NSArray *objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:self.topicsChoiceSegControl.selectedSegmentIndex], nil];
NSDictionary * dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:@"segmentChosen" object:nil userInfo:dict];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(segmentChosen:) name:@"segmentChosen" object:nil];

-(void) segmentChosen:(NSNotification *)notification {
    NSNumber *segmentChosenNum = [[notification userInfo] valueForKey:@"segmentChosen"];
}

我知道这本身并不能回答您的问题,但它确实为您试图克服的问题提供了另一种解决方案。

您能详细说明一下吗?如果我理解你的问题,那么我会使用
[tableView reloadData]
主视图控制器的
视图上每次都会出现
,你能编辑你的问题,将头文件和实现文件中的代码都包括进来吗?引用_tableChoiceToDisplay和chooseTopic?看起来很有希望,Matt,但实例变量是什么?我确实尝试过使用[tableView reloadData],但没有成功。我已经在我的DVC中使用了此代码:MasterViewController*mvc=[[MasterViewController alloc]init];self.delegate=mvc;我也在做类似的事情。在DVC中,我有一个函数chooseTopic,它调用MVC中一个名为changeTopicTo的函数。我的分段控制有三个部分,微观、宏观和国际。ChooseTopic将信息从分段控件发送到changeTopicTo函数。ChangeTopicTo说:(NSString*)ChangeTopicTo:(NSString*)主题{u tableChoiceToDisplay=topic;return{u tableChoiceToDisplay;}NSLog告诉我{u tableChoiceToDisplay正在工作,因为它读“Micro”,“Macro”,等等。但是如果我尝试NSLog for _tableChoiceToDisplayin viewDidDisplay,它会给我空值。我无法格式化上述内容。基本上,我正在向MVC获取信息,但无法从ViewWillDisplay访问它。我正在更改的变量可以从MVC中的函数访问。它是_tableChoiceToDisplay,是一个字符串。但是当我在视图中使用NSLog时,它会显示_tableChoiceToDisplay为空。你能发布更多代码吗?我想检查包含和操作
\u tableChoiceToDisplay