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



我能够像预期的那样在swift中做到这一点,但在objective 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)
}
}

我试过在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,只是基类UIViewController.
你告诉实例是UIViewController,那么你应该如何能够访问PremiumFeedbackViewController的特定属性?

你需要清楚地了解你在Swift中做什么来翻译它。

if let a = b as? SomeClass {
}

你正在测试a是否可以是SomeClass的一个实例。

在Objective-C中,这是:

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

if let feedbackNavVc =故事板? .instantiateViewController (标识符:"PremiumFeedbackNavViewController")吗?PremiumCustomNavigationController {如果让feedbackVc = feedbackNavVc.children.first作为?PremiumFeedbackViewController {feedbackVc。id = self.fileDetailsfeedbackVc。pageNumber = self.currentPageNumberfeedbackVc。pageCount = self.totalPageCountpresent(feedbackNavVc, animated: true, completion: nil)}}

应该是:

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)崩溃"-[someeclassofviewcontroller id]无法识别的选择器发送给instance"。它不会在(0)崩溃,因为feedbackNavVc,即使它不是PremiumCustomNavigationController的实例,它也是UIViewController(或nil,但没有问题)。UIViewController有一个属性childViewControllers,所以它是合法的。事实上,你不需要在这里(在Swift中也不需要)强制转换为PremiumCustomNavigationController

相关内容

  • 没有找到相关文章

最新更新