这是我完整的代码。 In PageViewController navigation not working
在这里,当我单击页面视图控制器的视图控制器中的按钮时,它将导航到下一个视图(最终视图控制器)。现在,我想更改导航栏的后退按钮。
当我单击ViewController中的按钮时,请参见上图[View controller将加载到PageViewController中]它将导航到FinalViewController
我的导航栏标题ViewController代码:
import Foundation
import Alamofire
import SwiftyJSON
class LoveltyAPI {
let loveltyURL = Bundle.main.object(forInfoDictionaryKey: "APIUrlString") as! String // Main URL
let buildVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String //infoDictionary?["CFBundleShortVersionString"] as AnyObject
weak var delegate:LoveltyProtocol?
func get_profile(app_user_id:String, token:String) {
let urlString = "\(loveltyURL)\(get_profile_string)?app_user_id=\(app_user_id)"
let headers = ["Content-Type":"application/json","X-Requested-With":"XMLHttpRequest", "Authentication":"Token \(token)"]
Alamofire.request(urlString, method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
let swiftyJsonVar = JSON(response.result.value!)
switch response.response?.statusCode {
case 200, 201:
self.delegate?.getUserProfile!(response: swiftyJsonVar["data"].dictionaryObject as AnyObject)
case 401:
self.delegate?.tokenExpired(response: tokenExpired as AnyObject)
case 404:
self.delegate?.serviceError!(response: swiftyJsonVar["message"] as AnyObject)
case 422:
self.delegate?.serviceError!(response: swiftyJsonVar["error"] as AnyObject)
case 503:
self.delegate?.appDisabled(response: swiftyJsonVar.dictionaryObject as AnyObject)
default:
self.delegate?.serviceError!(response: self.serverError as AnyObject)
}
case .failure(let error):
self.delegate?.serviceError!(response: self.serverError as AnyObject)
}
}
}
}
但是此代码不起作用
答案 0 :(得分:0)
后退按钮属于上一个视图控制器,而不是当前在屏幕上显示的那个。 你可以使用委托或segue 参见this
答案 1 :(得分:0)
这个答案很好用...
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "FVC")
let backButton = UIBarButtonItem()
backButton.title = "name"
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.self.navigationBar.topItem?.backBarButtonItem = backButton
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(storyboard!, animated: true)