[英]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.