我能夠按預期快速完成,但是在目標 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 中分配子視圖控制器屬性?
uj5u.com熱心網友回復:
在您嘗試的 Objective-C 代碼中,沒有提到PremiumCustomNavigationControlleror PremiumFeedbackViewController,只有基類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 = 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, animation: 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)顯示“-[SomeClassOfViewController id] unrecognized selector sent to instance”。它不會在 處崩潰(0),因為feedbackNavVc即使它不是 的實體PremiumCustomNavigationController,它也是一個UIViewController(或 nil,但沒有問題)。并且 aUIViewController有一個 property childViewControllers,所以它是合法的。事實上,你不需要強制轉換到PremiumCustomNavigationController這里(也不應該在 Swift 中)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329184.html
