我正在尝试使用 Storyboard 创建一个 UITableView,但最后我发现了一些可能很容易但我不知道如何解决的问题。 首先让我指出,我知道 Storyboard的局限性之一是您必须深入研究 Storyboard以查找有关您拥有的 View 的信息并将其链接到应用程序委托(delegate)。 我已经创建了我的可变数组和我将在应用程序委托(delegate)的表中使用的信息,现在我想将该 UITableView 引用到应用程序委托(delegate)。层次结构是这样的
- 首先我有 Root View ,一旦你点击一个按钮,它就会将你重定向到第二个 View
- 在第二个 View 中有另一个按钮,一旦你按下它,它就会将你重定向到 UINavigationController
- UINavigationController 包含 UITableView。
因此,如您所见,在导航控件和 UITableView 之前有两个 View 。
这是我尝试使用的代码,但它不起作用
UIViewController *viewController = (UIViewController *)self.window.rootviewController;
// The next line refers to the second view but does not work at all
UIViewController *secondView = [[UIViewController viewController] objectAtIndex:1];
//Then the following line is to redirect from the second view to the navigation controller
UINavigationController *navigationController =[[secondView viewController] objectAtIndex:0];
//Then is the table view
BuildingsViewController *buildingsViewController = [[navigationController viewControllers] objectAtIndex:0];
上面的代码不起作用。谁能帮帮我吗? 非常感谢
最佳答案
如果此代码位于应用程序委托(delegate)中,则可能有多种原因导致它无法正常工作。首先,您似乎将 View、ViewControllers 和 Navigation Controller 与您正在尝试做的事情混为一谈。其次,在您尝试执行此操作时,无法保证所有 View / View Controller 都已创建或以最终构建 View Controller 呈现时的方式连接。
您可以尝试在您的 BuildingsViewController(您说的是您的 TableView Controller )中尝试使用
获取 App Delegate 的句柄MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
一旦您有了委托(delegate)的句柄,您就可以简单地引用您在 BuildingsViewController 中创建的可变数组结构等。
例如在“numberOfRowsInSection”方法中:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
return [myBuildings count];
或者在 cellForRowAtIndexPath 方法中:
// something like this but using your names for app delegate, building array and the accessor for the building name
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
cell.textLabel.text = [myBuildings objectAtIndex indexPath.row].theBuildingName;
关于iphone - 使用 Storyboard对应用程序委托(delegate)的引用 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448938/