Подтвердить что ты не робот

Отключить и представить контроллер просмотра в Swift

Привет, я пытаюсь представить viewcontroller и отклонить мой текущий вид модальности, но этот код не работает.
self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                self.presentViewController(vc!, animated: true, completion: nil)
            })

наоборот тоже не работает над блоком завершения текущего управления просмотром

EDIT: заменить vc! к себе

4b9b3361

Ответ 1

Вы должны получить viewController, который представил self (текущий ViewController). Если этот контроллер представлений является rootViewController, который можно использовать, как показано ниже, если не этот запрос, он основан на вашей иерархии диспетчера.

if let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("vc3") as? ViewController3 {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window?.rootViewController!.presentViewController(vc3, animated: true, completion: nil)
}

Ответ 2

вы не можете этого сделать, потому что когда UIViewController A вызывает UIViewController B, а первый контроллер отклоняется, то два контроллера равны нулю.

У вас должен быть UIViewController в качестве базы, в этом случае MainViewController является базой. Вам необходимо использовать протокол для вызова навигации между контроллерами.

вы можете использовать протокол, скажем, например, как ниже: -

В вашем режиме настройки контроля доступа:

    protocol FirstViewControllerProtocol {
    func dismissViewController()
}

class FirstViewController: UIViewController {
    var delegate:FirstViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func goBack(sender: AnyObject) {
        self.dismissViewControllerAnimated(true) { 
            self.delegate!.dismissViewController()
        }
    }

Теперь в вашем основном контроллере просмотра

class MainViewController: UIViewController, FirstViewControllerProtocol {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func goToFirstViewController(sender: AnyObject) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(FirstViewController)) as! FirstViewController
    viewController.delegate = self
    self.presentViewController(viewController, animated: true, completion: nil)
}



//MARK: Protocol
func dismissViewController() {
    if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
        self.presentViewController(viewController, animated: true, completion: nil)
    }
}

Пример кода с раскадрой:


Ответ 3

Я думаю, что в вашем коде есть ошибка, где "я" должен быть представлением контроллера представления для представления "vc", а не "vc" его self

Ваш код

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                vc!.presentViewController(vc!, animated: true, completion: nil)
            })

Попробуйте это

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                self.presentViewController(vc!, animated: true, completion: nil)
            })

надеюсь, что это полезно

Ответ 4

let parent = self.parentViewController!

parent.dismissViewControllerAnimated(true, completion: {
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
            parent.presentViewController(vc!, animated: true, completion: nil)
        })

Ответ 5

Здесь решение для Swift3

Чтобы представить ViewController

let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController

self.present(NotificationVC, animated: true, completion: nil)

Чтобы отклонить ViewController:

self.dismiss(animated: true, completion: nil)

Ответ 6

if let vc = storyboard?.instantiateViewController(withIdentifier: "IdOfYourVC") {

    present(vc, animated: true, completion: nil)
}