需要将用于实例化的视图控制器的扩展包装在调度主线程中,但是遇到了该错误,如何解决该错误的任何想法?
extension UIStoryboard {
convenience init(name: StoryboardName) {
self.init(name: name.rawValue, bundle: nil)
}
func instantiateVC<T: UIViewController>(identifier: String = T.identifier) -> T {
// swiftlint:disable force_cast
DispatchQueue.main.async {
let controller = self.instantiateViewController(withIdentifier: identifier) as! T
controller.removeBackButtonTitle()
return controller
}
// swiftlint:enable force_cast
}
func instantiateInitialVC() -> UIViewController {
return self.instantiateInitialViewController()!
}
}
答案 0 :(得分:1)
尝试仅删除主线程上的按钮标题是否足够
func instantiateVC<T: UIViewController>(identifier: String = T.Identifier) -> T {
let controller = self.instantiateViewController(withIdentifier: identifier) as! T
DispatchQueue.main.async {
controller.removeButtonTitle()
}
return controller
}
答案 1 :(得分:0)
instantiateVC
不应返回值。您需要向此函数添加新参数以传递controller
:
func instantiateVC<T: UIViewController>(identifier: String = T.identifier, completion: @escaping (T) -> Void) {
// swiftlint:disable force_cast
DispatchQueue.main.async {
let controller = self.instantiateViewController(withIdentifier: identifier) as! T
controller.removeBackButtonTitle()
completion(controller)
}
// swiftlint:enable force_cast
}