Swift - 覆盖扩展中的现有objc方法

时间:2016-02-08 17:28:11

标签: ios swift uiviewcontroller

所以,我觉得我对Swift非常熟悉,但事实证明,我不是。

我正在尝试创建UINavigationControllerUIViewController扩展,并覆盖一些方法来简化对设备轮换的控制。

所以,我发现在Swift中它仍然可能: https://stackoverflow.com/a/28220616/982550 但由于我不想创建基础 UIViewController类,我认为我遵循与Obj-C相同的方法:

extension UINavigationController {

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return topViewController!.supportedInterfaceOrientations()
}

public override func shouldAutorotate() -> Bool {
    return topViewController!.shouldAutorotate()
}

override public func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return topViewController!.preferredInterfaceOrientationForPresentation()

    }
}

extension UIViewController {

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait
}

public override func shouldAutorotate() -> Bool {
    return false
}

override public func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return .Portrait
    }
}

但它不能用XCode 7.2.1和Swift 2.1编译,我得到了:

  

:方法'supportedInterfaceOrientations()'用   Objective-C选择器'supportedInterfaceOrientations'与。冲突   具有相同Objective-C选择器的先前声明

我想没有办法绕过它,因为这两个类都是UIViewController的子类我不能用相同的方法进行2次扩展?

0 个答案:

没有答案