如何使用条件转场
Posted
技术标签:
【中文标题】如何使用条件转场【英文标题】:how to work with conditional segue 【发布时间】:2017-06-29 10:21:38 【问题描述】:我一直在尝试根据特定条件实现条件转换,但我不知道我落后的地方。如果有人可以帮助我,那将不胜感激
以下是代码:-
有两个 segues 需要处理,segueOtp
& segueError
@IBAction func btnToOtpScreen(_ sender: Any)
let providedMarketingRef = txtMarketingRef.text
//let providedhopName = txtShopName.text
if((providedMarketingRef?.isEmpty)!)
displayAlertMessage(messageToDisplay: "Enter Your Marketing Reference")
else
let myUrl = URL(string: "http://kumbhkaran.co.in/ios_otp_check/request_sms.php");
//let myUrl = URL(string: "http://kumbhkaran.co.in/OTPCheck/request_sms.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "email=\(Email)&mobile=\(UserName)&name=\(ShopName)"
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
//spinningActivity!.hide(true)
if error != nil
self.displayAlertMessage(messageToDisplay: error!.localizedDescription)
return
do
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json
var msg : String!
//getting json response
msg = parseJSON["message"] as! String?
print(msg)
if(msg == "SMS request is initiated! You will be receiving it shortly.")
//self.performSegue(withIdentifier: "segueOTP", sender: self)
self.GlobalSegue="segueOTP"
else if(msg == "Sorry! Error occurred in registration.")
//self.performSegue(withIdentifier: "segueError", sender: self)
self.GlobalSegue="segueError"
else if(msg == "Mobile number already existed!")
//self.performSegue(withIdentifier: "segueError", sender: self)
self.GlobalSegue="segueError"
else if(msg == "Sorry! mobile number is not valid or missing.")
//self.performSegue(withIdentifier: "segueError", sender: self)
self.GlobalSegue="segueError"
catch
print(error)
task.resume()
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if (GlobalSegue=="segueOTP")
let destViewController : OTPScreen = segue.destination as! OTPScreen
destViewController.Category = Category
destViewController.SubCategory = SubCategory
destViewController.ShopName = ShopName
destViewController.ManagerName = ManagerName
destViewController.ManagerMobile = ManagerMobile
destViewController.UserName = UserName
destViewController.Landline = Landline
destViewController.Email = Email
destViewController.Website = Website
destViewController.City = City
destViewController.Address = Address
destViewController.Area = Area
destViewController.Pincode = Pincode
destViewController.StartTime = StartTime
destViewController.EndTime = EndTime
destViewController.ShopStatus = ShopStatus
destViewController.HomeDelivery = HomeConsultancy
destViewController.Password = Password
destViewController.MarketingRef = txtMarketingRef.text!
destViewController.ImgShopName = ShopName
destViewController.OtherCategory = Other
destViewController.WorkerCount = WorkerCount
destViewController.latitude = lblLatitude.text!
destViewController.longitude = lblLongitude.text!
else
var _ : ErrorController = segue.destination as! ErrorController
//ErrorController 和 Congratulations 是两个不同的视图,我想根据条件转移。
【问题讨论】:
您的问题到底是什么?发生什么了?你想发生什么? 检查 if else 条件,那些 segue 不起作用。应该在不同的条件下调用两个不同的segue 设置断点并单步执行您的代码。看起来不错,但可能没有满足您的条件。 你在prepareForSegue
做了什么?你能把代码分享给我们吗?
我已经分享了prepareForSegue的代码
【参考方案1】:
你必须通过它的标识符来区分segues:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
switch segue.identifier
case "segueOTP":
let destViewController = segue.destination as! OTPScreen
destViewController.Category = Category
destViewController.SubCategory = SubCategory
destViewController.ShopName = ShopName
destViewController.ManagerName = ManagerName
destViewController.ManagerMobile = ManagerMobile
destViewController.UserName = UserName
destViewController.Landline = Landline
destViewController.Email = Email
destViewController.Website = Website
destViewController.City = City
destViewController.Address = Address
destViewController.Area = Area
destViewController.Pincode = Pincode
destViewController.StartTime = StartTime
destViewController.EndTime = EndTime
destViewController.ShopStatus = ShopStatus
destViewController.HomeDelivery = HomeConsultancy
destViewController.Password = Password
destViewController.MarketingRef = txtMarketingRef.text!
destViewController.ImgShopName = ShopName
destViewController.OtherCategory = Other
destViewController.WorkerCount = WorkerCount
destViewController.latitude = lblLatitude.text!
destViewController.longitude = lblLongitude.text!
case "segueError":
var _ = segue.destination as! ErrorController
default : break
注意:
请遵守变量名以小写字母开头的命名约定。
【讨论】:
以上是关于如何使用条件转场的主要内容,如果未能解决你的问题,请参考以下文章