我一直想着我遇到的一种依赖。我是Objective-C的新手,所以请放轻松我。我甚至不知道如何谷歌我遇到的问题。
我有三个班级:
RootViewController
HttpRequestWrapper
ManagementClass
RootViewController
继承ManagementClass
的位置。
(@interface RootViewController : ManagementClass <UINavigationControllerDelegate>
)。
所以在ManagementClass
我称这个函数为:
[[self.navigationController topViewController] getTableData];
其中topViewController
是RootViewController
(但稍后我想为任何topViewController
更改它。)
此功能getTableData
调用HttpRequestWrapper
并在其URLConnection
中调用其所有代理。但是当谈到委托方法时,(void)connectionDidFinishLoading:(NSURLConnection *)connection
我希望NSNotification
通知RootViewController
从请求中加载数据并使用数据填充表。但是NSNotification
即使在导航堆栈上是RootViewController
,也不会通知topViewController
。
所以我的问题是如何将数据从URLConnection
恢复回RootViewController
,即使请求是通过ManagementClass
以某种方式初始化的。
以下是我的问题的一些代码:
班级RootViewController
:
#import "RootViewController.h"
......
// Get the data using the class HttpRequstWrapper (where I wrap the request in NSURLConnection)
- (void) getTableData{
httpRequestWrapper = [HttpRequestWrapper alloc];
[httpRequestWrapper getXMLDataWithURL:[NSString stringWithFormat:@"equipment/xml"]];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Get Table Data
[self getTableData];
}
- (void) populateTableData{
// Maybe change this line that error is shown if no data is found
if (httpRequestWrapper.dataFromTheHttpRequest == nil){
NSLog(@"Data got from the HttpRequestWrapper is nil or empty");
}
// Parse the returned data
xmlcont = [[XMLController alloc] loadXMLByURL:httpRequestWrapper.dataFromTheHttpRequest];
// Get the names of the Equipments as names of each Section
arrayEquipments = [NSMutableArray alloc];
arrayEquipments = xmlcont.equipments;
sectionsTitles = [[NSMutableArray alloc]init];
for (Equipment *eq in arrayEquipments){
[sectionsTitles addObject: eq.equipmentName];
}
// Reload the data in TableView
[self.tableView reloadData];
}
- (void) viewDidLoad{
....
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(populateTableData) name:@"connectionFinishedLoading" object:nil];
}
班级ManagementClass
:
在.h文件中
@interface ManagementClass : UITableViewController {}
<。>文件中的
@implementation ManagementClass
- (void) refreshPage {
if ([[self.navigationController topViewController] isKindOfClass: [RootViewController class]]){
[[self.navigationController topViewController] getTableData];
}
}
在HttpRequestWrapper
课程中我有:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
......
if ([urlExtension rangeOfString: @"notification"].location != NSNotFound) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoading1" object:nil];
} else if ([urlExtension rangeOfString: @"execution/workflow"].location != NSNotFound) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoadingPhaseView" object:nil]; execution/workflow
} else if ([urlExtension rangeOfString: @"equipment/xml"].location != NSNotFound) {
//[[self.navigationController topViewController] populateTableData]; - not working
[[NSNotificationCenter defaultCenter] postNotificationName:@"connectionFinishedLoading" object:nil];
}
.......
}
我想要一种让控制器尽可能重用的方法。
我想到[[self.navigationController topViewController] populateTableData]
放在connectionDidFinishLoading
但导航控制器无法从HttpRequstWrapper
调用,它根本就不执行。我仍然无法弄清楚为什么。我只能使用目前可见的navigationController
的{{1}}执行ViewController
方法。
答案 0 :(得分:0)
从我看到的,你正在RootViewController中监听“connectionFinishedLoading”,你在HttpRequestWrapper中发布了“connectionFinishedLoading1”。它们必须与RootViewController
相同