Swift: prepareForSegue with two different segues

你。 提交于 2019-12-01 00:01:27

问题


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.

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).

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)


回答1:


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:

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
    }
}



回答2:


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
        }
    }
}



回答3:


You should compare the segue.identifier with the string not segue:

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.




回答4:


prepareForSegue with two different segues when you click on button uialert controller will show it will give you three options 1. physical, 2. online and 3. cancel.

@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)
    }
}



回答5:


In order to navigate different segues in objective C you need to follow these actions.

  1. Click on the actual segue on storyboard
  2. Go to attribute inspector and rename the identifier of the segue
  3. customize the code as 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");
    }
    


来源:https://stackoverflow.com/questions/31457300/swift-prepareforsegue-with-two-different-segues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!