【问题标题】:Conflicting return type in implementation of 'supportedInterfaceOrientations': - Warning'supportedInterfaceOrientations' 实现中的返回类型冲突: - 警告
【发布时间】:2015-09-21 15:39:45
【问题描述】:

升级到 Xcode 7.0 后,我在 UIViewControllerRotation 方法中收到警告:- (NSUInteger)supportedInterfaceOrientations:

实现中的返回类型冲突 'supportedInterfaceOrientations':'UIInterfaceOrientationMask'(又名 “枚举 UIInterfaceOrientationMask”)与“NSUInteger”(又名“无符号” int')

为什么会这样,我该如何解决?

编辑: 如果你去定义你会看到返回类型实际上已经改变了: - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); 但更改代码中的返回类型不会使警告静音。

【问题讨论】:

  • 永远不要让警告静音(除非您是一位专家,他自己回答了为什么这是一个好主意)。修复代码。
  • @gnasher729 OP 显然意味着“平息”或“满足”警告,而不是完全消除它。

标签: ios objective-c xcode


【解决方案1】:

试试这个调整:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000  
- (NSUInteger)supportedInterfaceOrientations  
#else  
- (UIInterfaceOrientationMask)supportedInterfaceOrientations  
#endif  
{
   return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

  • 仅当代码库需要同时使用 Xcode 6 和 7 编译时才执行此操作。如果您完全切换到 Xcode 7,则只需使用 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  • @rmaddy 是XCODE版本还是IOS版本?
  • @nurne 真的。 iOS 版本决定了 API 的可用性,但 Xcode 版本定义了所使用的 Base SDK。
  • @rmaddy 你能详细说明一下吗?
  • @QED 定义了什么,比如UIInterfaceOrientationMask__IPHONE_9_0是基于Xcode版本的,但是iOS版本决定了是否有东西可用,比如暗模式,所以需要#available .我知道已经 3.5 年了,所以如果你现在能比我解释得更好,请随意!
【解决方案2】:

我正在使用这个:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
#define supportedInterfaceOrientationsReturnType NSUInteger
#else
#define supportedInterfaceOrientationsReturnType UIInterfaceOrientationMask
#endif

- (supportedInterfaceOrientationsReturnType)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

...我认为比 Nishant 的修复要长一点,但更清晰一些。

【讨论】:

  • __IPHONE_9_0 没有在 pre-iOS-9 Xcodes 中定义(技术上,iOS SDK 安装在 Developer/Platforms/iPhoneOS.platform/Developer/SDKs 下)所以除非你使用 Xcode 7,否则它不会编译。@Nishant's使用 int 文字的解决方案是解决此问题的方法。如果不是这样,您可以查看__IPHONE_OS_VERSION_MAX_ALLOWED &gt; __IPHONE_8_4,但这要脆弱得多,仍然需要安装 iOS 8.4 SDK(Xcode 6.4;在 Xcode 6.3.2 中不起作用)。
  • @SlippD.Thompson 好的。感谢您的澄清。
  • 也就是说,您仍然可以完成这项工作,但您必须自己定义__IPHONE_9_0。 IE。在您的 PCH 中,执行:#ifndef __IPHONE_9_0 [newline] #define __IPHONE_9_0 90000 [newline] #endif
【解决方案3】:

我已经为此工作了几个周末以找出正确的解决方案,我尝试了许多其他解决方案,但都无法正常工作。如果您只希望某些 UI 处于横向或纵向,请尝试以下方法。我正在运行 Xcode 7.2.1,我使用 Bool 来设置每个类中的值以及 NSUSerDefaults 我想遵循特定的 UIInterfaceOrientations。每次出现新的 UI 或设备旋转时,都会调用下面的方法,您可以通过在该方法中放置一个 NSLog 检查 bool 的值来检查这一点,并亲自查看它是如何变化的。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:

在您的 AppDelegate 中执行以下操作

.h @property (assign, nonatomic) BOOL shouldRotate;

.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED{


_shouldRotate = [[NSUserDefaults standardUserDefaults]boolForKey:@"rotateKey"];
NSLog(@"Did I get to InterfaceOrientation \n And the Bool is %d",_shouldRotate);

if (self.shouldRotate == YES)
            return UIInterfaceOrientationMaskAllButUpsideDown;
        else
            return UIInterfaceOrientationMaskPortrait;

}

现在在每个班级中,您都希望有一个特定的 UI 界面来执行此操作

 -(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:YES];

   // [[AppDelegate sharedAppDel]setShouldRotate:YES];
    BOOL rotate = NO;
    [[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
    [[NSUserDefaults standardUserDefaults]synchronize];

}
-(BOOL)shouldAutorotate
{
    return YES;
}

在要旋转的视图中,将 Bool 值更改为 Yes。 现在在你的 AppDelegate 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    BOOL rotate = NO;
    [[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
    [[NSUserDefaults standardUserDefaults]synchronize];


    return YES;
}

也在你的 AppDelegate 中

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    BOOL rotate = NO;
    [[NSUserDefaults standardUserDefaults]setBool:rotate forKey:@"rotateKey"];
    [[NSUserDefaults standardUserDefaults]synchronize]; 
}

希望这对许多开发人员有所帮助。 经过多次测试和测试。

问候

JZ

【讨论】:

  • 你的答案有点混乱。我的整个项目处于纵向模式,我只希望标签栏控制器中的单个视图自动旋转。如何做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
相关资源
最近更新 更多