xcode Swift:prepareForSegue 有两个不同的 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31457300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: prepareForSegue with two different segues
提问by Ragnar
I have 2 UIButtons
on a view.
Each of them are linked to 2 different views.
They have to pass different data depending which button you just tapped.
我有 2UIButtons
个视图。它们中的每一个都链接到 2 个不同的视图。他们必须根据您刚刚点击的按钮传递不同的数据。
I know there is different way to segue
but I want to use the prepareForSegue
version for each (seems more clear for me to have the segue
drawn on the storyboard and some code to explain what happened).
我知道有不同的方法,segue
但我想prepareForSegue
为每个版本使用版本(对我来说segue
在故事板上绘制和一些代码来解释发生的事情似乎更清楚)。
I tried this...
我试过这个...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue != "historySegue") {
let controller = segue.destinationViewController as! ResultViewController
controller.match = self.match
} else {
let controller = segue.destinationViewController as! HistoryViewController
controller.history = self.history
}
}
@IBAction func showHistory(sender: UIButton) {
performSegueWithIdentifier("historySegue", sender: self)
}
@IBAction func match(sender: UIButton) {
performSegueWithIdentifier("matchSegue", sender: self)
}
But I got an error when I click the buttons
但是当我点击按钮时出现错误
(lldb)
(lldb)
回答by Rachel Harvey
Take out your IBActions (making sure to delete them in the Connections Inspector via the right side bar in the Interface Builder as well as in your code). For the segues, just use the Interface Builder, make sure both segue's identifiers are correct, then try this prepareForSegue method:
取出您的 IBActions(确保通过 Interface Builder 中的右侧栏以及您的代码在 Connections Inspector 中删除它们)。对于 segue,只需使用 Interface Builder,确保两个 segue 的标识符都正确,然后试试这个 prepareForSegue 方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "matchSegue" {
let controller = segue.destinationViewController as! ResultViewController
controller.match = self.match
} else if segue.identifier == "historySegue" {
let controller = segue.destinationViewController as! HistoryViewController
controller.history = self.history
}
}
回答by Fogmeister
You could switch it...
你可以换...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "matchSegue":
let controller = segue.destinationViewController as! ResultViewController
controller.match = self.match
case "historySegue":
let controller = segue.destinationViewController as! HistoryViewController
controller.history = self.history
}
}
}
回答by Greg
You should compare the segue.identifier
with the string not segue:
您应该将segue.identifier
与字符串 not segue进行比较:
segue.identifier == "historySegue"
segue.identifier == "historySegue"
It won't fix your problem but it helps later.
它不会解决您的问题,但稍后会有所帮助。
I believe you have added an breakpoint and your app just stop at it. Remove the breakepoint and it will work.
我相信您已经添加了一个断点,而您的应用程序就停在了它上面。删除断点,它将起作用。
回答by Akbar Khan
prepareForSegue with two different segueswhen you click on button uialert controller will show it will give you three options 1. physical, 2. online and 3. cancel.
当您单击按钮时,prepareForSegue 带有两个不同的 segue, uialert 控制器将显示它会给您三个选项:1. 物理、2. 在线和 3. 取消。
@objc func twoSegues(sender : UIButton) {
let alert = UIAlertController(title: "Update Request", message: "Are you sure to update the record!", preferredStyle: .alert)
let yes = UIAlertAction(title: "Yes", style: .default, handler: { [weak self] (UIAlertAction) in
if(serviceType == "physical") {
DispatchQueue.main.async {
self?.performSegue(withIdentifier: "physical", sender: self?.myArray[id!].serviceID)
}
}
if(serviceType == "online") {
DispatchQueue.main.async {
self?.performSegue(withIdentifier: "online", sender: self?.myArray[id!].serviceID)
}
}
})
let no = UIAlertAction(title: "No", style: .default, handler: nil)
alert.addAction(yes)
alert.addAction(no)
self.present(alert, animated: true, completion: nil)
}
}
回答by avivamg
In order to navigate different segues in objective Cyou need to follow these actions.
为了在目标 C 中导航不同的 segue,您需要遵循这些操作。
- Click on the actual segueon storyboard
- Go to attribute inspector and rename the identifierof the segue
customize the codeas following, here's an example:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"segueNumberOne"]){ YourViewController *functionalityViewController = [segue destinationViewController]; functionalityViewController.someProperty = someValue; NSLog(@"move to segue number 1"); }else if ([segue.identifier isEqualToString:@"segueNumberTwo"]){ NSLog(@"move to segue number 2"); }
- 单击情节提要上的实际转场
- 转到属性检查器并重命名segue的标识符
自定义代码如下,这是一个例子:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"segueNumberOne"]){ YourViewController *functionalityViewController = [segue destinationViewController]; functionalityViewController.someProperty = someValue; NSLog(@"move to segue number 1"); }else if ([segue.identifier isEqualToString:@"segueNumberTwo"]){ NSLog(@"move to segue number 2"); }