How to check if a view controller can perform a segue

前端 未结 8 1978
长发绾君心
长发绾君心 2020-12-29 02:10

This might be a very simple question but didn\'t yield any results when searching for it so here it is...

I am trying to work out a way to check if a certain view co

相关标签:
8条回答
  • 2020-12-29 02:40
    - (BOOL)canPerformSegueWithIdentifier:(NSString *)identifier
    {
        NSArray *segueTemplates = [self valueForKey:@"storyboardSegueTemplates"];
        NSArray *filteredArray = [segueTemplates filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"identifier = %@", identifier]];
        return filteredArray.count>0;
    }
    
    0 讨论(0)
  • 2020-12-29 02:41

    Swift version of Evgeny Mikhaylov's answer, which worked for me:

    I reuse a controller for two views. This helps me reuse code.

    if(canPerformSegueWithIdentifier("segueFoo")) {
      self.performSegueWithIdentifier("segueFoo", sender: nil)
    }
    else {
      self.performSegueWithIdentifier("segueBar", sender: nil)
    }
    
    
    func canPerformSegueWithIdentifier(identifier: NSString) -> Bool {
        let templates:NSArray = self.valueForKey("storyboardSegueTemplates") as! NSArray
        let predicate:NSPredicate = NSPredicate(format: "identifier=%@", identifier)
    
        let filteredtemplates = templates.filteredArrayUsingPredicate(predicate)
        return (filteredtemplates.count>0)
    }
    
    0 讨论(0)
  • 2020-12-29 02:42

    As stated in the documentation:

    Apps normally do not need to trigger segues directly. Instead, you configure an object in Interface Builder associated with the view controller, such as a control embedded in its view hierarchy, to trigger the segue. However, you can call this method to trigger a segue programmatically, perhaps in response to some action that cannot be specified in the storyboard resource file. For example, you might call it from a custom action handler used to process shake or accelerometer events.

    The view controller that receives this message must have been loaded from a storyboard. If the view controller does not have an associated storyboard, perhaps because you allocated and initialized it yourself, this method throws an exception.

    That being said, when you trigger the segue, normally it's because it's assumed that the UIViewController will be able to respond to it with a specific segue's identifier. I also agree with Dan F, you should try to avoid situations where an exception could be thrown. As the reason for you not to be able to do something like this:

    if ([self canPerformSegueWithIdentifier:@"SegueID"])
        [self performSegueWithIdentifier:@"SegueID"];
    

    I am guessing that:

    1. respondsToSelector: only checks if you are able to handle that message in runtime. In this case you can, because the class UIViewController is able to respond to performSegueWithIdentifier:sender:. To actually check if a method is able to handle a message with certain parameters, I guess it would be impossible, because in order to determine if it's possible it has to actually run it and when doing that the NSInvalidArgumentException will rise.
    2. To actually create what you suggested, it would be helpful to receive a list of segue's id that the UIViewController is associated with. From the UIViewController documentation, I wasn't able to find anything that looks like that

    As for now, I am guessing your best bet it's to keep going with the @try @catch @finally.

    0 讨论(0)
  • 2020-12-29 02:48

    To check whether the segue existed or not, I simply surrounded the call with a try-and-catch block. Please see the code example below:

    @try {
        [self performSegueWithIdentifier:[dictionary valueForKey:@"segue"] sender:self];
    }
    @catch (NSException *exception) {
        NSLog(@"Segue not found: %@", exception);
    }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-29 02:51

    Reference CanPerformSegue.swift

    import UIKit
    
    extension UIViewController{
        func canPerformSegue(identifier: String) -> Bool {
            guard let identifiers = value(forKey: "storyboardSegueTemplates") as? [NSObject] else {
                return false
            }
            let canPerform = identifiers.contains { (object) -> Bool in
                if let id = object.value(forKey: "_identifier") as? String {
                    return id == identifier
                }else{
                    return false
                }
            }
            return canPerform
        }
    }
    
    0 讨论(0)
  • 2020-12-29 02:56

    This post has been updated for Swift 4.


    Here is a more correct Swift way to check if a segue exists:

    extension UIViewController {
    func canPerformSegue(withIdentifier id: String) -> Bool {
            guard let segues = self.value(forKey: "storyboardSegueTemplates") as? [NSObject] else { return false }
            return segues.first { $0.value(forKey: "identifier") as? String == id } != nil
        }
    
        /// Performs segue with passed identifier, if self can perform it.
        func performSegueIfPossible(id: String?, sender: AnyObject? = nil) {
            guard let id = id, canPerformSegue(withIdentifier: id) else { return }
            self.performSegue(withIdentifier: id, sender: sender)
        }
    }
    
    // 1
    if canPerformSegue("test") {
        performSegueIfPossible(id: "test") // or with sender: , sender: ...)
    }
    
    // 2
    performSegueIfPossible(id: "test") // or with sender: , sender: ...)
    
    0 讨论(0)
提交回复
热议问题