web-dev-qa-db-fra.com

Comment envoyer et présenter à UIViewController par programme sans séparation dans iOS Swift 3

J'utilise ce code pour Push [~ # ~] afficher [~ # ~] et [~ # ~] modalement [~ # ~] par programme dans iOSObjectif C.
Et maintenant vous voulez savoir sur Swift 3.

NewsDetailsViewController *vc =  instantiateViewControllerWithIdentifier:@"NewsDetailsVCID"];
vc.newsObj = newsObj;
//--this(SHOW)
[self.navigationController pushViewController:vc animated:YES];  
//-- or this(MODAL)
[self presentViewController:vc animated:YES completion:nil];  

Je vous remercie.

23
reza_khalafi

Push

fais comme

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController 
 vc.newsObj = newsObj
 navigationController?.pushViewController(vc,
 animated: true)

ou plus sûr

  if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
        viewController.newsObj = newsObj
        if let navigator = navigationController {
            navigator.pushViewController(viewController, animated: true)
        }
    }

présent

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
      vc.newsObj = newsObj
           present(vc!, animated: true, completion: nil)  

ou plus sûr

   if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
     {

     vc.newsObj = newsObj
    present(vc, animated: true, completion: nil)
    }
82
Anbu.Karthik

De manière élégante.

Créer un protocole Navigatable:

protocol Navigatable {
    /// Storyboard name where this view controller exists.
    static var storyboardName: String { get }


    /// Storyboard Id of this view controller.
    static var storyboardId: String { get }

    /// Returns a new instance created from Storyboard identifiers.
    static func instantiateFromStoryboard() -> Self
}

Créez une implémentation de contrôleur instanciée par défaut:

/**
 Extension of Navigatable protocol with default implementations.
 */
extension Navigatable {
    static func instantiateFromStoryboard() -> Self {
        let storyboard = UIStoryboard(name: self.storyboardName, bundle: nil)
        guard
            let viewController = storyboard
                .instantiateViewController(withIdentifier: self.storyboardId) as? Self else {
                    fatalError("Cannot instantiate the controller.")
        }

        return viewController
    }
}

Étend le UIViewController à Push un contrôleur de vue:

extension UIViewController {
    /**
     Pushes a view controller of the provided type.

     - Parameter viewControllerType: Type of view controller to Push.
     - Parameter completion: Function to be executed on completion.
     Contains the view controller that was pushed when successful and nil otherwise.
     */
    func pushViewControllerOfType<T: Navigatable>(viewControllerType: T.Type, completion: (T) -> Void) {
        let viewController = T.instantiateFromStoryboard()
        if let vc = viewController as? UIViewController {
            self.pushViewController(vc, animated: true)
        }
        completion(viewController)
    }


    /**
     Pushes a view controller of the provided type.

     - Parameter viewControllerType: Type of view controller to Push.
     */
    func pushViewControllerOfType<T: Navigatable>(viewControllerType: T.Type) {
        self.pushViewControllerOfType(viewControllerType: viewControllerType) { _ in }
    }
}

Ensuite, vous pouvez utiliser le protocole Navigatable pour un contrôleur de vue spécifique.

class MySuperViewController {
   override func viewDidLoad() {
      ...
   }
   // ...
}
extension MySuperViewController: Navigatable {
    static var storyboardName: String {
        return "Main"
    }

    static var storyboardId: String {
        return "MySuperViewControllerId" // From your story board name Main
    }
}

// Instantiate your controller
let vc = MySuperViewController.instantiateFromStoryboard()

// Or
//
// Push your view controller

// testViewController.Swift

self.pushViewControllerOfType(viewControllerType: MySuperViewController)
11
YMonnier