How to set parentViewController in UIViewController?

前端 未结 6 830
春和景丽
春和景丽 2021-02-13 10:26

The parentViewController property of UIViewController is readonly, but I am nesting custom view controllers and would like to use this property.

However, since it is re

相关标签:
6条回答
  • 2021-02-13 10:42

    It looks like calling the setParentViewController: method works.

    [childViewController setParentViewController:self];
    

    However this still generates a compiler Warning. Which, IMO, means don't do it (I subclassed instead).

    0 讨论(0)
  • 2021-02-13 10:46

    I tried Alex's suggestion buy making a category for UIViewController, and it worked in the simulator, but not on my phone. here is the category

    @interface UIViewController (parentSetter) 
    -(void)setParentUIViewController:(UIViewController*)parent;
    @end
    
    @implementation UIViewController (parentSetter)
    -(void)setParentUIViewController:(UIViewController*)parent
    {
     _parentViewController = parent;
    }
    @end
    

    It compiles and works fine, but note the underscore member which is a bit off putting. That's what causes the linker error when compiling against the 3.0 SDK.

    I have a container view that has 2 subviews and a table is one of them. The table needs a parent so it can interact with the navigation bar, among other things.

    I'm going with this solution instead:

    @interface AdoptedTableViewController : UITableViewController {
        UIViewController* surrogateParent;
    }
    
    -(UINavigationController*)navigationController;
    @property (nonatomic, assign) IBOutlet UIViewController *surrogateParent;
    @end
    
    @implementation AdoptedTableViewController
    @synthesize surrogateParent;
    
    -(UINavigationController*)navigationController
    {
        if( [super navigationController] )//self.navigationController ) 
        {
            return [super navigationController];
        }
        else
        {
            return surrogateParent.navigationController;
        }
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    All of my table view controllers are now adoptedTableViewControllers. The main reason they need parents is so they can push view controllers on to the navigation stack, so that is handled transparently by the navigation controller getter.

    It would be nice if parentViewController were not read only, but in my dabble with _parentViewController I discovered there is more to the ViewController hierarchy than just that property. I think there might be a lot of coupling and responsibilities in that relationship that Apple hasn't cleaned up enough for the masses. For instance, I noticed an odd deselection behavior when moving up the navigation hierarchy that I couldn't fix. Perhaps UINavigation controllers reflect the class of their top controller and behave differently?

    In short, it really is read only and there is no clean or simple workaround. You just got to architect around it.

    0 讨论(0)
  • 2021-02-13 10:51

    Solution is:

       - (void)setParentController:(UIViewController*)parent{
     [self setValue:parent forKey:@"_parentViewController"];
        }
    

    It does not cause problems with linker!

    PS: Don't use "setParentViewController" as method name, because this method exists in private API and Apple say: "The non-public API that is included in your application is setParentViewController:.

    If you have defined a method in your source code with the same name as the above mentioned API, we suggest altering your method name so that it no longer collides with Apple's private API to avoid your application being flagged with future submissions.

    Please resolve this issue in your next update..."

    0 讨论(0)
  • 2021-02-13 10:53

    However, since it is readonly, and I found no other way to set this property, my quesion is: how do I set it?

    If it is readonly, you can't use dot notation without getting a compiler error.

    However, you might be able to use categories to add a custom modifyParentViewController method to the UIViewController class.

    Even if the property is readonly, the variable itself might be modifiable, if it is not @protected. If it is @protected, subclassing the view controller may allow you the option of modifying the variable.

    0 讨论(0)
  • 2021-02-13 10:53

    I realize this question was asked before iOS 5, but for the reference, you should use addChildViewController when you want to nest UIViewControllers. This will also automatically set the parentViewController property.

    - (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);
    

    You can read more about "Creating Custom Content View Controllers" at Apple.

    0 讨论(0)
  • 2021-02-13 11:07

    parentViewController is for the purposes of NavigationViewControllers and presenting modal view controllers , there is no way to set the property without the methods pushViewController or presentModalViewController. The parentViewController is a readonly property can only be read by the UIViewController class and UINavigationController class, subclasses of these will not have access to set the property.

    0 讨论(0)
提交回复
热议问题