简体   繁体   中英

Is it possible to delay and/or cancel a Show Detail segue for UISplitViewController?

I would like to use a UISplitViewController to show a list of connectable devices in the master view, and to show information retrieved from a selected device in the detail view.

To do that, when the user taps on a device, I need to attempt to connect to that device. If unsuccessful, there'd be nothing to show and I would display an error message. If successful, I'd read data from the device and display that in the detail view.

The Show Detail segue from the UISplitViewController seems to automatically fire immediately after an entry in the master view is tapped. Can I intercept this somehow to add the necessary logic for connection/read? If not, what is an alternative method for me to do this?

In your ViewController you can override shouldPerformSegueWithIdentifier

class Foo: UIViewController {
    func doAsynchStuff(completion: (canProceed:Bool)->()) {
    // ...
    }

    override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
        if everythingOK {
            return true
        } else {
            // do your stuff
            doAsynchStuff({ (canProceed) -> () in
                if canProceed {
                    self.performSegueWithIdentifier(identifier, sender: sender)
                } else {
                    // present error message
                }
            })
            return false
        }
    }
}

If you return true the segue will be execute, otherwise you:

  1. run your asynchronous code
  2. invoke performSegueWithIdentifier and when the asynch function has completed you decide what you do looking at the result
  3. return false to cancel the current segue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM