将 RootViewController 分配给 UIViewController 会发出警告 - 为什么?

发布于 2024-12-06 00:26:14 字数 933 浏览 0 评论 0原文

我想在其类之一中访问我的应用程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2024-12-13 00:26:14

您可以将对象分配给声明为其超类的变量。当您只想在一组自己的子类上使用超类方法时,这没有问题,并且非常有用,尤其是当下一个视图控制器的特定类型未知时,导航堆栈中的视图控制器很常见。

也想想吧。 类似的方法

[self presentModalViewController:customController animated:YES];

如果不能做到这一点, 就不会起作用。此方法被声明为采用 UIViewController * 但您传入自定义 UIViewController 子类,没有任何抱怨。最后

[rootViewController isKindOfClass:[UIViewController class]];

会返回YES。量子ED。

您是否在应用程序委托的标头中转发声明了您的类 RootViewController ?

@class RootViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate> {
....

你拼写正确吗?这是一个常见的错误输入区域,因为 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

[self presentModalViewController:customController animated:YES];

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

[rootViewController isKindOfClass:[UIViewController class]];

will return YES. QED.

Have you forward declared your class RootViewController in the header for your app delegate?
i.e.

@class RootViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate> {
....

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.

多情癖 2024-12-13 00:26:14

问题是 RootViewControllerUIViewController 不是同一个类。

在 AppDelegate 中,您将 viewController 声明为 RootViewController 类型。然后,在这些行中:

AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController* presentingViewController = appDelegate.viewController;

您将创建 presentingViewController,其类型为 UIViewController,并将其设置为 RootViewController 的实例。这就是错误的根源。

通过使用一致的类型来修复此问题。

阅读 RootViewController、AppDelegate 和我可以创建的视图控制器类? 很好地解释了这两种类型之间的差异。

The problem is that RootViewController is not the same class as UIViewController.

In the AppDelegate, you declare viewController to be of type RootViewController. Then, in these lines:

AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController* presentingViewController = appDelegate.viewController;

You are creating presentingViewController, which is of type UIViewController, and setting it to an instance of RootViewController. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文