gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:16:13 32 4
gpt4 key购买 nike

它是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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com