【问题标题】:Warning: passing incompatible type to setRootViewController:警告:将不兼容的类型传递给 setRootViewController:
【发布时间】:2011-09-26 17:05:50
【问题描述】:

我正在为 iPhone 做一个计算器,在设备旋转时,会显示一个新视图,我收到以下三个错误:

CalculatorAppDelegate.m:21:警告:不兼容的 Objective-C 类型“struct CalculatorViewController *”,当从不同的 Objective-C 类型传递“setRootViewController:”的参数 1 时,需要“struct UIViewController *”`

此错误(及以下)来自代码:

self.window.rootViewController = self.viewController;

CalculatorAppDelegate.m:警告:语义问题:从“CalculatorViewController *”分配给“UIViewController *”的指针类型不兼容

以下错误来自:

UIInterfaceOrientation interfaceOrientation = [[object object] orientation];`

CalculatorViewController.m:警告:语义问题:从枚举类型“UIDeviceOrientation”隐式转换为不同的枚举类型“UIInterfaceOrientation”

【问题讨论】:

  • 看起来您的CalculatorViewController 不是UIViewController 的子类。您应该仔细检查您的 CalculatorViewController.h 文件。

标签: ios cocoa-touch uiviewcontroller uideviceorientation


【解决方案1】:

对于您的前 2 个错误:

  • CalculatorViewController 未在您的 .h 文件中声明为 UIViewController 的子类
  • 或者编译器不知道它,因为你忘记了代码中的#import "CalculatorViewController.h" 让编译器知道它的定义

对于您的上一期,这是因为您误用了UIDeviceOrientationUIInterfaceOrientation,它们不是同一类型(甚至是完全相关的)。

  • UIInterfaceOrientation 定义用户界面的方向。它只有 4 个值:Portrait、Landscape Left、Landscape Right 或 UpsideDown。
  • UIDeviceOrientation 定义设备 的方向。它有 6 个值,定义您的设备是笔直向上、向左或向右旋转 90°、倒置或朝下。

如果您将界面设置为 旋转(shouldAutorotateToInterfaceOrientation: 仅对 4 个 UIInterfaceOrientation 之一返回 YES),您的 UIDeviceOrientation 仍然可以更改(没有什么可以阻止用户将他/她的 iPhone 转到任何位置),但您的 UIInterfaceOrientation 不会改变。

如果你设置你的界面使其旋转shouldAutorotateToInterfaceOrientation:总是返回YES),当用户将他/她的iPhone向右转动时,UIDeviceOrientation将是@ 987654334@ 因为 iPhone 会右转...而 UIInterfaceOrientation 会是UIInterfaceOrientationLandscapeLeft,因为界面方向会向左旋转 90°,所以因为设备向右转动,界面仍将水平显示给用户。

您会注意到UIInterfaceOrientationUIDeviceOrientation 具有相反的值(对于两者共有的枚举;当然对于UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown 没有对应的UIInterfaceOrientation )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2018-05-23
    • 2020-08-30
    • 2011-01-10
    • 2020-07-29
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多