创建后如何更改 ViewController 的类型?

发布于 2025-01-06 10:44:04 字数 899 浏览 4 评论 0原文

我有一个程序,根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2025-01-13 10:44:04

您可以执行以下任一操作:

SettingsTableViewController *leftControllerCasted = (SettingsTableViewController *)leftController;
SettingsDetailViewController *rightControllerCasted = (SettingsDetailViewController *)rightController;
leftControllerCasted.masterVC = self;
rightControllerCasted.tableVC = leftController; // Doesn't matter if you assign it to leftController or leftControllerCasted

或者

((SettingsTableViewController *)leftController).masterVC = self;
((SettingsDetailViewController *)rightController).tableVC = leftController;

您已正确创建对象,但当您为它们分配 UIViewController 指针对象类型时,编译器不知道您的对象属于您的自定义类。

You can do either of the following:

SettingsTableViewController *leftControllerCasted = (SettingsTableViewController *)leftController;
SettingsDetailViewController *rightControllerCasted = (SettingsDetailViewController *)rightController;
leftControllerCasted.masterVC = self;
rightControllerCasted.tableVC = leftController; // Doesn't matter if you assign it to leftController or leftControllerCasted

or

((SettingsTableViewController *)leftController).masterVC = self;
((SettingsDetailViewController *)rightController).tableVC = leftController;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文