【发布时间】: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;vc和PremiumCustomnavigationController逻辑。 -
有一个
if let来确保课程,你可以用isKindOfClass:测试做的事情。 -
我是 Objective C 的新手,如果您可以添加可以标记为答案且不复杂的答案。
-
标题应该是:如何在Objective-C中转换指针
标签: ios swift objective-c xcode uiviewcontroller