Home > database >  Not able to assign child VC properties in Objective C
Not able to assign child VC properties in Objective C

Time:09-29

I was able to do it in swift as expected but where as same functionality is required to do in objective C not able to set child VC properties.

Here is the swift code it works as expected.

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

I've tried to do it in objective C but not able to set properties inside child VC. If we can convert above swift code to objective C that would be fine.

            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];

How to assign child view controller properties in objective C?

CodePudding user response:

In the Objective-C code you tried, there is no mention of PremiumCustomNavigationController or PremiumFeedbackViewController, just the base class UIViewController.
You are telling the instance are UIViewController, so how should you be able to acess specific properties of PremiumFeedbackViewController?

You need to clearly understand what you are doing in Swift to translate it.

if let a = b as? SomeClass {
}

You are testing if a can be an instance of SomeClass.

So in Objective-C, this would be:

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, animated: true, completion: nil) } }

Should be then:

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

Now, if you are sure about the classes, you can skip the isKindOfClass: and directly put the cast.

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

If you aren't sure about your classes, it will crash at (1) with "-[SomeClassOfViewController id] unrecognized selector sent to instance". It won't crash at (0), because feedbackNavVc, even if it's not an instance of PremiumCustomNavigationController, it's a UIViewController (or nil, but then no issue). And a UIViewController has a property childViewControllers, so it's legit. In fact, you shouldn't need to cast to PremiumCustomNavigationController here (nor in Swift).

  • Related