创建后如何更改 ViewController 的类型?
我有一个程序,根据 UITableView 中选择的行在 UISplitView 中创建 UIViewController。例如,如果用户单击名为“settings”的行,程序将创建一个名为 settingsTableVC 的视图控制器(显示在 UISplitViewController 的左侧窗格中)和一个名为 settingsDetailVC 的详细视图控制器,该控制器显示在右侧窗格中。
逻辑工作正常,但我在初始 tableView 中的每一行都有很多代码,用于创建和设置每个 ViewController 及其公共属性,在我看来,一种更简单的方法是定义两个通用 ViewController,然后进行强制转换将它们转换为方法中的正确类型。这样,我可以一般地定义它们的公共属性(因为它们基本上都是相同的属性,指向 masterViewController 和 tableViewController 的指针)
EG
UIViewController *leftController = nil;
UIViewController *rightController = nil;'
然后,在 didSelectRowAtIndexPath 中:
leftController = [[SettingTableViewController alloc]init];
rightController = [[SettingsDetailViewController alloc]init];
leftController.masterVC = self;
rightController.tableVC = leftController;
但是,编译器警告 - 'UIViewController 没有声明属性 masterVC' 。
如何在运行时更改这些 ViewController 的类?
I have a program that creates UIViewControllers in a UISplitView depending upon which row is selected in a UITableView. For example, if the user clicks the row called 'settings', the program creates a view controller called settingsTableVC (which displays in the left-hand pane of the UISplitViewController) and a detail view controller called settingsDetailVC, which appears in the right hand pane.
The logic works fine, but I have a lot of code for each row in the initial tableView, for creating and setting up each ViewController, and their public properties and it seems to me a simpler way would be to define two generic ViewControllers, then cast them to the correct type in a method. That way, I can define their public properties generically (as they are all basically the same properties, pointers to the masterViewController and the tableViewController)
E.G.
UIViewController *leftController = nil;
UIViewController *rightController = nil;'
Then, in the didSelectRowAtIndexPath:
leftController = [[SettingTableViewController alloc]init];
rightController = [[SettingsDetailViewController alloc]init];
leftController.masterVC = self;
rightController.tableVC = leftController;
However, the complier warns - 'UIViewController does not declare a property masterVC'.
How can I change the class of these ViewControllers at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下任一操作:
或者
您已正确创建对象,但当您为它们分配
UIViewController
指针对象类型时,编译器不知道您的对象属于您的自定义类。You can do either of the following:
or
You have created your objects correctly, but as you assign them a
UIViewController
pointer object type the compiler doesn't know your objects are of your custom class.