[英]Custom Segue in Swift
@objc(SEPushNoAnimationSegue)
class SEPushNoAnimationSegue: UIStoryboardSegue {
override func perform () {
self.sourceViewController.navigationController.pushViewController(self.destinationViewController, animated:false)
}
}
In the above code, I have 2 questions: 1). 在上面的代码中,我有两个问题:1)。 it has a compile error: 'UINavigationController!'
它有一个编译错误:'UINavigationController!' does not have a member named 'pushViewController'
没有名为'pushViewController'的成员
But in that class, it did has a pushViewController method. 但是在那个类中,它确实有一个pushViewController方法。
2). 2)。 I have to add the annotation: @objc(SEPushNoAnimationSegue), otherwise, in storyboard, it only recognize the random generated name, like, _tcxxxxSEPushNoAnimationSegue.
我必须添加注释:@objc(SEPushNoAnimationSegue),否则,在storyboard中,它只识别随机生成的名称,如_tcxxxxSEPushNoAnimationSegue。
why these 2 issues happen here? 为什么这两个问题发生在这里?
UIStoryboardSegue has an irritating flaw: its sourceViewController and destinationViewController properties are typed as AnyObject!
UIStoryboardSegue有一个令人恼火的缺陷:它的sourceViewController和destinationViewController属性被输入为
AnyObject!
(that's the case even in Objective-C (Id type)) and not as UIViewController
, as it should be. (即使在Objective-C(Id类型)中也是这种情况)而不是
UIViewController
,因为它应该是。
That same flaw creates havoc in your perfect and simple code. 同样的缺陷会在您完美而简单的代码中造成严重破坏。 Here's how to rewrite it in order to fix the compile errors:
以下是如何重写它以修复编译错误:
@objc(SEPushNoAnimationSegue)
class SEPushNoAnimationSegue: UIStoryboardSegue {
override func perform () {
let src = self.sourceViewController as UIViewController
let dst = self.destinationViewController as UIViewController
src.navigationController.pushViewController(dst, animated:false)
}
}
NOTE: Apple fixed this thing in iOS 9. sourceViewController
and destinationViewController
are now correctly declared as UIViewController
. 注意:Apple在iOS 9中修复了这个问题
sourceViewController
和destinationViewController
现在正确地声明为UIViewController
。
The Swift compiler stores its symbols using its own name mangling , and good ol' Objective-C does not recognize it in Xcode. Swift编译器使用自己的名称mangling来存储它的符号,并且好的'Objective-C在Xcode中无法识别它。 Using an explicit
@obj()
solves the issue. 使用显式的
@obj()
解决了这个问题。
This works fine for me 这对我来说很好
@objc(SEPushNoAnimationSegue) class SEPushNoAnimationSegue: UIStoryboardSegue {
override func perform() {
let sourceViewController = self.sourceViewController as UIViewController
let destinationViewController = self.destinationViewController as UIViewController
sourceViewController.presentViewController(destinationViewController, animated: true, completion: nil)
}
}
Even Better: 更好:
import UIKit
class CustomSegue: UIStoryboardSegue {
override func perform() {
self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
}
}
Swift 3.0: Swift 3.0:
import UIKit
class CustomNoAnimationSegue: UIStoryboardSegue {
override func perform() {
if let navigation = source.navigationController {
navigation.pushViewController(destination, animated: false)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.