Swift UI Special Training 42 use Swift code to implement page Jump and pass value, uiswift
Previously, if you want to click a button to jump to another button, sometimes we need to trigger page jump through some actions, such as shaking the mobile phone, in this way, you cannot directly use the link method to achieve the jump. How can you achieve the jump through code? The transition method is still used. In the storyboard, link the two pages to be redirected. Note that it is no longer limited to connecting objects such as buttons to another page, as long as the jump link is from one to the other, connect the two pages and select show as the link. This is not demonstrated here. Be sure to give this seuge a name, this is because we will see it later. Here we name it showDetail.
. For example, I need to shake my phone to jump to the next page and rewrite the following method on the first page:
Override func shouldw.mseguewithidentifier (identifier: String ?, Sender: AnyObject ?) -> Bool {if self. showArray. count! = 0 & self. markpoint! = 0 // your own judgment condition {return true} else {return false }}
The above method is used to determine whether the page can be redirected and add our own judgment conditions to it.
Then, in the method of capturing and shaking the mobile phone action (if you do not understand the captured code, you can refer to the article I wrote earlier and have an introduction), add our judgment jump conditions. If the jump is true, if the jump condition is not met, give your own warning:
Override func motionEnded (motion: UIEventSubtype, withEvent event: UIEvent) {if motion = UIEventSubtype. motionShake {if markpoint = 1 & self. searchpoint = false {// No jump is allowed. A warning is given: var alertController1 = UIAlertController (title: "No matching dishes! ", Message: nil, preferredStyle :. alert) var cancelAction = UIAlertAction (title: "return", style :. cancel, handler: nil) alertController1.addAction (cancelAction) self. presentViewController (alertController1, animated: true, completion: nil)} if self. searchpoint = true {// you can jump to self. extends mseguewithidentifier ("showDetail", sender: self) // jump to the next page and identify "showDetail "}}}
The ingress mseguewithidentifier method is used for redirection. If you want to use the transport board prepareForSegue, my code is as follows:
Override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject ?) {If segue. identifier = "showDetail" {let destinationController = segue. destinationViewController as ShowTableViewController destinationController. navigationItem. title = "Shake to the dish" destinationController. tempCaipin = self. showArray // pass value }}
The first page:
Jump after shaking the phone: