【问题标题】:Not able to assign child VC properties in Objective C无法在 Objective C 中分配子 VC 属性
【发布时间】:2021-11-20 16:53:03
【问题描述】:

我能够按预期快速完成,但是在目标 C 中需要相同的功能时无法设置子 VC 属性。

这是它按预期工作的快速代码。

    if let feedbackNavVc =
        storyboard?.instantiateViewController(
            identifier: "PremiumFeedbackNavViewController"
        ) as? PremiumCustomNavigationController {
        if let feedbackVc = feedbackNavVc.children.first as? PremiumFeedbackViewController {
            feedbackVc.id = self.fileDetails?.id
            feedbackVc.pageNumber = self.currentPageNumber
            feedbackVc.pageCount = self.totalPageCount
            present(feedbackNavVc, animated: true, completion: nil)
        }
    }

我尝试在目标 C 中执行此操作,但无法在子 VC 中设置属性。如果我们可以将上面的 swift 代码转换为目标 C 就好了。

            NSString * storyboardName = @"Premium";
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
            UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"PremiumFeedbackNavViewController"];
            UIViewController * feedbackVC = vc.childViewControllers.firstObject;
            //feedbackVC.id = self.objectId;  ///Error: Property id not found on object of type UIViewController
            [self presentViewController:vc animated:YES completion:nil];

如何在目标 C 中分配子视图控制器属性?

【问题讨论】:

  • as?,这是一个演员表。所以UIViewController * feedbackVC = vc.childViewControllers.firstObject; 应该是PremiumFeedbackViewController * feedbackVC = (PremiumFeedbackViewController *) vc.childViewControllers.firstObject; vcPremiumCustomnavigationController 逻辑。
  • 有一个if let 来确保课程,你可以用isKindOfClass: 测试做的事情。
  • 我是 Objective C 的新手,如果您可以添加可以标记为答案且不复杂的答案。
  • 标题应该是:如何在Objective-C中转换指针

标签: ios swift objective-c xcode uiviewcontroller


【解决方案1】:

在您尝试的 Objective-C 代码中,没有提到 PremiumCustomNavigationControllerPremiumFeedbackViewController,只是基类 UIViewController
您告诉实例是UIViewController,那么您应该如何访问PremiumFeedbackViewController 的特定属性?

你需要清楚地了解你在 Swift 中所做的事情才能翻译它。

if let a = b as? SomeClass {
}

您正在测试a 是否可以是SomeClass 的实例。

所以在 Objective-C 中,这将是:

if ([a isKindOfClass:[SomeClass class]]) {
    SomeClass *b = (SomeClass *)a;
}

如果让反馈NavVc = 故事板?.instantiateViewController( 标识符:“PremiumFeedbackNavViewController” ) 作为? PremiumCustomNavigationController { if let feedbackVc = feedbackNavVc.children.first as? PremiumFeedbackViewController { 反馈Vc.id = self.fileDetails?.id 反馈Vc.pageNumber = self.currentPageNumber 反馈Vc.pageCount = self.totalPageCount 存在(反馈NavVc,动画:真,完成:无) } }

应该是:

UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"PremiumFeedbackNavViewController"];

if ([vc isKindOfClass: [PremiumCustomNavigationController class]]) {
    PremiumCustomNavigationController *feedbackNavVc =  (PremiumCustomNavigationController *)vc;
    UIViewController *firstChild = [feedbackNavVc.childViewControllers firstObject];
    if ([firstChild isKindOfClass: [PremiumFeedbackViewController class]]) {
        PremiumFeedbackViewController *feedbackVc = (PremiumFeedbackViewController *)firstChild;
        feedbackVc.id = self.fileDetails.id
        feedbackVc.pageNumber = self.currentPageNumber
        feedbackVc.pageCount = self.totalPageCount
    }
}

现在,如果你对课程有把握,可以跳过isKindOfClass:,直接放演员表。

PremiumCustomNavigationController *feedbackNavVc = [storyboard instantiateViewControllerWithIdentifier:@"PremiumFeedbackNavViewController"];
PremiumFeedbackViewController *feedbackVc = [feedbackNavVc. childViewControllers firstObject]; // (0)
feedbackVc.id = self.fileDetails.id // (1)
feedbackVc.pageNumber = self.currentPageNumber
feedbackVc.pageCount = self.totalPageCount

如果你不确定你的类,它会在(1) 崩溃,并带有“-[SomeClassOfViewController id] unrecognized selector sent to instance”。 它不会在(0) 崩溃,因为feedbackNavVc,即使它不是PremiumCustomNavigationController 的实例,它也是UIViewController(或nil,但没有问题)。而UIViewController 有一个属性childViewControllers,所以它是合法的。事实上,您不需要在此处转换为 PremiumCustomNavigationController(在 Swift 中也不行)。

【讨论】:

  • 在这部分代码 [feedbackNavVc.children firstObject] 中,在类型为“PremiumCustomNavigationController”的对象上找不到属性“子项”,因此出现错误;
  • 应该修复。它在 Objective-C 中被命名为childViewControllers,我忘了。您在尝试中正确地写了它。
  • 我做到了这个 [feedbackNavVc.childViewControllers firstObject];然后错误出现在 ///错误:在 PremiumFeedbackViewController 类型的对象上找不到属性 ID,我不需要 pageNumber 和 pageCount 只需要 id。
  • PremiumFeedbackViewController的声明是什么? id 可见吗?啊,它应该重命名,因为id 是一个受保护的术语。桥文件怎么说:“YourAppName-Swift.h”应该在某处提到PremiumFeedbackViewController...
  • 我可以得到 VC 的 outlets 作为 feedbackVc.pageNoLabel 但不是声明的属性 id 声明为 open var id: String!
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多