[英]How to perform action before triggering exit segue?
I have created a segue (present modally) from view controller A to view controller B that is triggered through a button in the navigation controller of view controller A. The modal that takes over is used to send a friend request by typing in an email. 我已经创建了一个从视图控制器A到视图控制器B的segue(以模态形式呈现),该视图通过视图控制器A的导航控制器中的按钮触发。接管的模式用于通过键入电子邮件来发送朋友请求。 When a user types in the email and presses a button to submit the friend request, I want to perform an action triggered by the button (calling a server to send the friend request and returning success if the email exists or returning error if the email doesn't exist).
当用户键入电子邮件并按下按钮以提交朋友请求时,我想执行由按钮触发的操作(调用服务器以发送朋友请求,如果电子邮件存在则返回成功,如果电子邮件不存在则返回错误不存在)。 If success, I want to exit/unwind the segue back to A. If error, I don't want to exit/unwind the segue.
如果成功,我想退出/退避到A。如果出错,我不想退出/退避。
I have looked into this question, but it doesn't seem to have the implementation of what I need. 我已经研究了这个问题,但是似乎并没有实现我所需要的。 I am trying the following:
我正在尝试以下方法:
class BViewController: UIViewController {
// The button function
@IBAction func sendFriendRequest(sender: AnyObject) {
println("Button Function.")
self.performSegueWithIdentifier("SendFriendRequest", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendFriendRequest" {
println("Preparing for segue.")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And then in first view controller: 然后在第一个视图控制器中:
AViewController: UIViewController {
// The unwind function
@IBAction func saveFriendRequest(segue:UIStoryboardSegue) {
println("Finished the segue.")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is the result when I set up the exit segue for the button (ctrl-drag the button to 'Exit' in the ViewController), I get the calls in this order: 这是当我为按钮设置退出按钮(在ViewController中将ctrl拖动到“退出”按钮)时的结果,我按以下顺序获得调用:
Preparing for segue.
Finished the segue.
Button Function.
However, I don't necessarily want to call the prepareForSegue or the unwind function every time, only if the server gives a success message. 但是,我不必每次都仅在服务器给出成功消息时才调用prepareForSegue或unwind函数。 So how would I connect things in the storyboard so that when I call performSegueWithIdentifier, I don't have to have call the other functions (prepareForSegue, etc.)?
因此,如何连接情节提要中的内容,以便当我调用performSegueWithIdentifier时,不必调用其他函数(prepareForSegue等)?
If you want to the action to perform before the segue, create and if-statement that determines that the action was completed successfully, then perform segue. 如果要让操作在segue之前执行,请创建并确定操作成功完成的if语句,然后执行segue。 ie.
即。
if (ActionSuccess) {
self.performSegueWithIdentifier("Segue", sender: nil)
}
Remove the exit segue from the button and instead simply link the button to a new IBAction and insert 从按钮上删除退出序列,而是简单地将按钮链接到新的IBAction并插入
self.dismissViewControllerAnimated(true, completion: nil)
So, your new code would be: 因此,您的新代码将是:
@IBAction func saveFriendRequest(sender: AnyObject) {
//Do your stuff to save the friend request
self.dismissViewControllerAnimated(true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.