UINavigationController return 无 Swift
UINavigationController return nil Swift
当我在 viewWillApear
或 viewDidLoad
中尝试 print(self.navigationController)
时,一切正常。但是当委托 return 来自 API print(self.navigationController)
return nil
的响应时。可能是什么?
extension EnterpriseList: APIDataDelegate {
func successRequest() { //print(self.navigtionController) == nil
DispatchQueue.main.async {
self.navigationController?.popToRootViewController(animated: true)
}
}
func badRequest() {
DispatchQueue.main.async {
Alert.showWarningAlert(withTitle: "Внимание!", andMessage: "Ошибка получения данных, попробуйте чуть позже", whereSender: self)
}
}
}
class EnterpriseList: UIViewController {
let dataSource = DataSource()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
dataSource.makeAPICall()
}
}
extension EnterpriseList: APIDataDelegate {
func successRequest() {
DispatchQueue.main.async {
self.navigationController!.popToRootViewController(animated: true)
}
}
func badRequest() {
DispatchQueue.main.async {
}
}
}
protocol APIDataDelegate: class {
func successRequest()
func badRequest()
}
class DataSource {
var delegate: APIDataDelegate?
private let queue = DispatchQueue(label: "Test", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: nil)
func makeAPICall() {
queue.asyncAfter(deadline: .now() + 2) {[weak self] in
self?.delegate?.successRequest()
}
queue.asyncAfter(deadline: .now() + 10) {[weak self] in
self?.delegate?.successRequest()
}
}
}
崩溃是因为您在已释放的对象上调用方法。
解决方法如下:
让你的代表很弱,你正在正确地释放对象
weak var delegate: APIDataDelegate?
感谢@Manoj 的回答,它对我有帮助。但我在其他地方找到了解决方案。除了 SurveyList
,我还有 UserMenu
,我在其中创建了 static let
视图控制器。删除 static
我解决了我的问题。
当我在 viewWillApear
或 viewDidLoad
中尝试 print(self.navigationController)
时,一切正常。但是当委托 return 来自 API print(self.navigationController)
return nil
的响应时。可能是什么?
extension EnterpriseList: APIDataDelegate {
func successRequest() { //print(self.navigtionController) == nil
DispatchQueue.main.async {
self.navigationController?.popToRootViewController(animated: true)
}
}
func badRequest() {
DispatchQueue.main.async {
Alert.showWarningAlert(withTitle: "Внимание!", andMessage: "Ошибка получения данных, попробуйте чуть позже", whereSender: self)
}
}
}
class EnterpriseList: UIViewController {
let dataSource = DataSource()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
dataSource.makeAPICall()
}
}
extension EnterpriseList: APIDataDelegate {
func successRequest() {
DispatchQueue.main.async {
self.navigationController!.popToRootViewController(animated: true)
}
}
func badRequest() {
DispatchQueue.main.async {
}
}
}
protocol APIDataDelegate: class {
func successRequest()
func badRequest()
}
class DataSource {
var delegate: APIDataDelegate?
private let queue = DispatchQueue(label: "Test", qos: .background, attributes: [], autoreleaseFrequency: .inherit, target: nil)
func makeAPICall() {
queue.asyncAfter(deadline: .now() + 2) {[weak self] in
self?.delegate?.successRequest()
}
queue.asyncAfter(deadline: .now() + 10) {[weak self] in
self?.delegate?.successRequest()
}
}
}
崩溃是因为您在已释放的对象上调用方法。
解决方法如下: 让你的代表很弱,你正在正确地释放对象
weak var delegate: APIDataDelegate?
感谢@Manoj 的回答,它对我有帮助。但我在其他地方找到了解决方案。除了 SurveyList
,我还有 UserMenu
,我在其中创建了 static let
视图控制器。删除 static
我解决了我的问题。