将 RootViewController 分配给 UIViewController 会发出警告 - 为什么?
我想在其类之一中访问我的应用程序的 RootViewController,以便呈现模式视图控制器。我通过获取 ApplicationDelegate 并请求它提供 RootViewController 并将其存储在 UIViewController 中来实现这一点。
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController* presentingViewController = appDelegate.viewController;
在我看来,这应该可以在没有警告的情况下工作,因为 RootViewController 继承自 UIViewController。但是我收到此警告:
不兼容的指针类型用“RootViewController *”类型的表达式初始化“UIViewController *__strong”
有人可以向我解释为什么我会看到此警告吗?
如果有帮助的话 - 这是我定义 RootViewController 的 AppDelegate:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}
@property (strong) RootViewController *viewController;
我像这样定义了我的 RootViewController:
@interface RootViewController : UIViewController {
}
I want to access the RootViewController of my App in one of its classes in order to present a modal view controller. I do this by getting the ApplicationDelegate and asking it for the RootViewController and store it in a UIViewController
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController* presentingViewController = appDelegate.viewController;
In my opinion this should work without a warning as RootViewController inherits from UIViewController. However I receive this warning:
Incompatible pointer types initializing 'UIViewController *__strong' with an expression of type 'RootViewController *'
Can someone explain to me why I see this warning?
If it helps - this is the AppDelegate where I define the RootViewController:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}
@property (strong) RootViewController *viewController;
I defined my RootViewController like this:
@interface RootViewController : UIViewController {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将对象分配给声明为其超类的变量。当您只想在一组自己的子类上使用超类方法时,这没有问题,并且非常有用,尤其是当下一个视图控制器的特定类型未知时,导航堆栈中的视图控制器很常见。
也想想吧。 类似的方法
如果不能做到这一点, 就不会起作用。此方法被声明为采用 UIViewController * 但您传入自定义 UIViewController 子类,没有任何抱怨。最后
会返回YES。量子ED。
您是否在应用程序委托的标头中转发声明了您的类 RootViewController ?
即
你拼写正确吗?这是一个常见的错误输入区域,因为 xCode 不会自动完成前向声明。然后它会自动补全头文件其余部分中的拼写错误。
您是否记得将 RootViewController 的头文件导入到 AppDelegate 的 .m 文件中?您仍然需要这样做,以便编译器了解继承性。
您的代码目前看起来是正确的,但我们没有全部代码。
You can assign an object to a variable declared as its superclass. That is no problem and is very useful when you only want to use superclass methods over a set of your own subclasses, especially common with view controllers in a navigation stack when the specific type of next view controller is unknown.
Also think about it. Methods like
wouldn't work without being able to do this. This method is declared as taking a UIViewController * but you pass in a custom UIViewController subclass with no complaints. Finally
will return YES. QED.
Have you forward declared your class RootViewController in the header for your app delegate?
i.e.
Did you spell it correctly? This is a common area to mistype as xCode doesn't autocomplete forward declarations. It will then autocomplete your typo in the rest of the header file.
Did you remember to import the header file for your RootViewController into the .m file for the AppDelegate? You will still need to do that so the compiler knows about the inheretance.
Your code looks correct at the moment but we don't have all of it.
问题是
RootViewController
与UIViewController
不是同一个类。在 AppDelegate 中,您将
viewController
声明为RootViewController
类型。然后,在这些行中:您将创建
presentingViewController
,其类型为UIViewController
,并将其设置为RootViewController
的实例。这就是错误的根源。通过使用一致的类型来修复此问题。
阅读 RootViewController、AppDelegate 和我可以创建的视图控制器类? 很好地解释了这两种类型之间的差异。
The problem is that
RootViewController
is not the same class asUIViewController
.In the AppDelegate, you declare
viewController
to be of typeRootViewController
. Then, in these lines:You are creating
presentingViewController
, which is of typeUIViewController
, and setting it to an instance ofRootViewController
. This is the source of the error.Fix this by using a consistent type.
Read What's the difference between the RootViewController, AppDelegate and the View Controller classes that I may create? for a nice explanation of the difference between these two types.