简体   繁体   English

重写-(void)parentViewController以返回自定义视图控制器。 并避免将来进行类型转换...?

[英]Override -(void)parentViewController to return custom view controller. And avoid type casting in future…?

I have a @interface CustomViewController : UIViewController . 我有一个@interface CustomViewController : UIViewController I wonder if it is possible to do this: 我想知道是否可以这样做:

-(CustomViewController*)parentViewController{
      return /* bla bla return custom vc here*/; 
}

As you can see, I want to simply override -(UIViewController*)parentViewController but to return an instance of CustomViewController . 如您所见,我只想重写-(UIViewController*)parentViewController但要返回CustomViewController的实例。

Obviously, I can simply create another property like customParentViewContrller and everything will be fine. 显然,我可以简单地创建另一个属性,例如customParentViewContrller ,一切都会好起来的。 However, I really don't want to do this, because I will end up using two properties, which actually represent the same thing - not good. 但是,我真的不想这样做,因为最终我将使用两个属性,它们实际上表示同一件事-不好。

Edit: Let me clear this. 编辑:让我清除这一点。 I want to avoid type casting in future so that this code [(CustomViewController*)self.parentViewController someCustomProperty] 我想避免将来再进行类型转换,以便此代码[(CustomViewController*)self.parentViewController someCustomProperty]

turns to this code 转到此代码

[self.parentViewController someCustomProperty] . [self.parentViewController someCustomProperty]

Yeah you can.. 是的,你可以..

Created a basic project and tested it out: 创建了一个基本项目并对其进行了测试:

- (ViewController *)parentViewController {
    return self;
}

Working fine. 工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何关闭当前视图控制器和父视图控制器? - How to dismiss current view controller and parentviewcontroller? 如何将自定义Popover视图控制器与Xamarin.iOS中的ParentViewController中心对齐? - How can I align my custom Popover view controller to center of ParentViewController in Xamarin.iOS? 从自定义视图内的UITextFields访问ParentViewController - Accessing ParentViewController from UITextFields inside a custom View 如何使用gmsAutocomplete视图控制器在uitextfield上应用自定义搜索以进行位置搜索? - How to apply custom search on uitextfield for place search using gmsAutocomplete view controller.? 嵌入自定义容器视图控制器时,内容位于导航栏下方。 - Content falls beneath navigation bar when embedded in custom container view controller. (Swift) 表视图 Controller。 未触发 cellForRowAt - (Swift) Table View Controller. cellForRowAt is not triggered 无法实例化视图控制器。 - 斯威夫特 - Failed to instantiate view controller. - Swift 意外删除的View Controller。 程序无法编译 - Accidently Deleted View Controller. Program will not compile 具有与ParentViewController不同的StatusBar样式的Modal View Controller - Modal View Controller with Different StatusBar Style than the ParentViewController 无法使用自定义类返回类型覆盖方法 - Cannot override method with a custom class return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM