【发布时间】:2015-04-09 23:13:18
【问题描述】:
我已经创建了一个从视图控制器 A 到视图控制器 B 的 segue(当前模态),它通过视图控制器 A 的导航控制器中的按钮触发。接管的模态用于通过键入发送朋友请求一封电邮。当用户输入电子邮件并按下按钮提交好友请求时,我想执行由按钮触发的操作(调用服务器发送好友请求,如果电子邮件存在则返回成功,如果电子邮件不存在则返回错误不存在)。如果成功,我想退出/展开segue回到A。如果错误,我不想退出/展开segue。
我已经研究了this 问题,但它似乎没有实现我所需要的。我正在尝试以下方法:
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.
}
}
然后在第一个视图控制器中:
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.
}
}
这是我为按钮设置退出 segue 时的结果(在 ViewController 中按住 ctrl 并拖动按钮到“退出”),我按以下顺序获得调用:
Preparing for segue.
Finished the segue.
Button Function.
但是,我不一定要每次都调用 prepareForSegue 或 unwind 函数,只要服务器给出成功消息。那么我将如何连接情节提要中的内容,以便在调用 performSegueWithIdentifier 时不必调用其他函数(prepareForSegue 等)?
【问题讨论】: