ios - Swift 中 navigationControllerSupportedInterfaceOrientations 的返回值是什么?

标签 ios swift interface uinavigationcontroller

它是UINavigationControllerDelegate的委托(delegate)方法。 Swift 和 Objective-C 的返回值是不同的。见:

objective-C :

 - (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController

swift

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> Int

当我使用 objective-c 时,它工作正常。但是,当我使用 Swift 时,我无法在方法中返回正确的值。见下图: enter image description here

谁能解释一下为什么,有什么解决办法吗?

最佳答案

尝试使用这个

func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController!) -> Int 
{
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}

更多详情请点击此处

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p281navigationInterface/ch19p615navigationInterface/ViewController.swift

强制转换的原因在 Objective-C 中

上面的函数返回类型是 NSInteger,因为 UIInterfaceOrientationPortrait 是一个 NSInteger 类型的枚举。

而Swift则是以struct的形式定义。

struct UIInterfaceOrientationMask : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue: UInt)

static var Portrait: UIInterfaceOrientationMask { get }
static var LandscapeLeft: UIInterfaceOrientationMask { get }
static var LandscapeRight: UIInterfaceOrientationMask { get }
static var PortraitUpsideDown: UIInterfaceOrientationMask { get }
static var Landscape: UIInterfaceOrientationMask { get }
static var All: UIInterfaceOrientationMask { get }
static var AllButUpsideDown: UIInterfaceOrientationMask { get }
}

https://books.google.co.in/books?id=5baVBQAAQBAJ&pg=PA277&lpg=PA277&dq=reason+of+UIInterfaceOrientationMask.Portrait.rawValue+using+rawValue&source=bl&ots=3X2wXD9HQc&sig=1Knh1LPBj2aoRMwuYAHUFjGdHxw&hl=en&sa=X&ei=0Z_6VPizEs7VuQTJz4KYBA&ved=0CC8Q6AEwAw#v=onepage&q=reason%20of%20UIInterfaceOrientationMask.Portrait.rawValue%20using%20rawValue&f=false

关于ios - Swift 中 navigationControllerSupportedInterfaceOrientations 的返回值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912170/

相关文章:

ios - 为什么我收到错误 EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

javascript - 动态键类型与 typescript 中的其他键类型

ios - 以困惑的方式将 NSMutableArray 元素添加到另一个 NSMutableArray

ios - 如何删除在 MDCTextField 外部点击时关闭键盘操作

ios - 32 位的应用程序可以在 64 位的 iPhone 5S 上运行吗?

swift - Kentico-cloud Swift SDK ContentType 没有可用的属性

ios - 如何将 Date(1440156888750-0700) 这样的日期转换为 Swift 可以处理的东西?

swift - 是否可以在枚举关联值条件和另一个枚举案例之间编写复合切换案例?

c# - 解决多接口(interface)实现

interface - 抽象如何实现接口(interface)?