iphone - 使用 Storyboard对应用程序委托(delegate)的引用 View

标签 iphone objective-c ios xcode ios5

我正在尝试使用 Storyboard 创建一个 UITableView,但最后我发现了一些可能很容易但我不知道如何解决的问题。 首先让我指出,我知道 Storyboard的局限性之一是您必须深入研究 Storyboard以查找有关您拥有的 View 的信息并将其链接到应用程序委托(delegate)。 我已经创建了我的可变数组和我将在应用程序委托(delegate)的表中使用的信息,现在我想将该 UITableView 引用到应用程序委托(delegate)。层次结构是这样的

  1. 首先我有 Root View ,一旦你点击一个按钮,它就会将你重定向到第二个 View
  2. 在第二个 View 中有另一个按钮,一旦你按下它,它就会将你重定向到 UINavigationController
  3. 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/

相关文章:

javascript - 是否有适用于 iOS 和 Android 的原生控件的 javascript 框架?

iphone - Xcode 中的代码签名错误

ios - UIImagePickerController 不会从/tmp/capture 中删除临时视频捕获文件

objective-c - 将 View 声明为属性 v. 临时创建

ios - 如何在 Objective-C 中的数组中找到 10 个值的平均值

ios - 在 SpriteKit 中检查节点和实例类型之间的碰撞

ios - 将 nil 分配给保留的属性会释放与其关联的对象吗?

iphone - Google map 着色 - iOS

ios - 无需键即可访问 NSMutableDictionary 值?

iphone - dataWithContentsOfURL 不检索数据