如何继续下一个 viewController
Posted
技术标签:
【中文标题】如何继续下一个 viewController【英文标题】:how to go on next viewController 【发布时间】:2016-06-13 07:01:34 【问题描述】:在我的应用程序中,我尝试为 api 调用创建不同的类。比如点击登录按钮并调用不同类的方法。但是当我想从那个不同的类转到另一个视图控制器时,它会崩溃。
这是我在 loginViewController 中的代码
let mydata = DataControllerLogin()
mydata.login(txtemail.text!,password: txtPassword.text!)
class DataControllerLogin: UIViewController
func login(username:String,password:String)
if Reachability.isConnectedToNetwork() == true
let url = "\(basicURL)login"
let param : [String : AnyObject] = [
"email" : username,
"password" : password
]
Alamofire.request(.POST, url, parameters: param, encoding: .JSON).responseObject(completionHandler: (response:Response<LoginCode, NSError>) in
if (response.result.value != nil)
let LoginCode = response.result.value
let message = LoginCode?.Message
let detail = LoginCode?.result
if (LoginCode?.Status == 1)
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
SVProgressHUD.dismiss()
else
alertViewShow(self, title: "Sorry", message: message!)
SVProgressHUD.dismiss()
if let threedayForecast = LoginCode?.result
print(threedayForecast.FirstName)
else
)
else
alertViewShow(self, title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
但是无论我定义了 viewController,它都会在线崩溃。
let controller : LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(controller, animated: true)
它的显示错误类似于
exc_bad_instruction (code=exc_i386_invop subcode=0x0)
如果你知道,请告诉我是什么问题?
【问题讨论】:
检查 LoginViewController 在情节提要中有标识符 LoginViewController? 已经检查过它的标识符是否相同 您的“DataControllerLogin”不在堆栈中或不在导航控制器中。 明白了,但我怎样才能更改我的代码,使其完美运行。 @Er.ShreyanshShah @Jack.Right :- 我在你的方法中又添加了一个参数。还有一个建议,请将“DataControllerLogin”类设为 NSObject 类。 【参考方案1】:把这个写在文件的顶部。
import UIKit
import Alamofire
import SwiftyJSON
typealias SOAPICompletionHandler = (code:Int, error:NSError?, response:NSDictionary?) -> Void
在您的文件中添加以下方法:-
func callApi(strApiName:String, param : [String : AnyObject]?, type:String, header:[String : String]?, completionHandler:SOAPICompletionHandler)
//let strURL : String = BASEURL+"/"+strApiName
let strURL = strApiName;
if type == POSTREQ
Alamofire.request(.POST, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary) as! AnyObject
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
else
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
)
else if type == GETREQ
Alamofire.request(.GET, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
else
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
)
else if type == PUTREQ
Alamofire.request(.PUT, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
else
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
)
else if type == DELETEREQ
Alamofire.request(.DELETE, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
else
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
)
else if type == PATCHREQ
Alamofire.request(.PATCH, strURL, parameters: param, encoding: .JSON, headers: header).responseJSON(completionHandler: (responseData) -> Void in
let isSuccess = JSON(responseData.result.isSuccess)
if isSuccess
// let swiftyJson = JSON(responseData.result.value! as! NSDictionary)
completionHandler(code: 1, error: nil, response: responseData.result.value! as? NSDictionary)
else
let error = responseData.result.error! as NSError
completionHandler(code: 0, error: error, response: nil)
)
你需要添加 Alamofire 和 SwiftyJSON。
希望最好。
【讨论】:
我不知道为什么,但它不允许我定义视图控制器。它给了我错误致命错误:在展开可选值时意外发现 nil以上是关于如何继续下一个 viewController的主要内容,如果未能解决你的问题,请参考以下文章