无法在 Objective C 中分配子 VC 属性

Not able to assign child VC properties in Objective C

我能够按预期在 swift 中执行此操作,但在 objective C 中需要执行相同功能的地方无法设置子 VC 属性。

这是按预期工作的 swift 代码。

    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)
        }
    }

我曾尝试在 objective C 中执行此操作,但无法在子 VC 中设置属性。如果我们可以将上面的 swift 代码转换为 objective 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];

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

在您尝试的 Objective-C 代码中,没有提到 PremiumCustomNavigationControllerPremiumFeedbackViewController,只是基础 class 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 { 如果让 feedbackVc = feedbackNavVc.children.first 为? PremiumFeedbackViewController { feedbackVc.id = self.fileDetails?.id feedbackVc.pageNumber = self.currentPageNumber feedbackVc.pageCount = self.totalPageCount 目前(feedbackNavVc,动画:真,完成:无) } }

那么应该是:

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
    }
}

现在,如果您对 class 有把握,可以跳过 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

如果您不确定您的 classes,它将在 (1) 处崩溃,并显示“-[SomeClassOfViewController id] 无法识别的选择器发送到实例”。 它不会在 (0) 处崩溃,因为 feedbackNavVc,即使它不是 PremiumCustomNavigationController 的实例,它也是 UIViewController(或零,但没有问题)。而一个 UIViewController 有一个 属性 childViewControllers,所以它是合法的。事实上,您不需要在此处转换为 PremiumCustomNavigationController(在 Swift 中也如此)。