IT story

Swift에서 ViewController를 해제하는 방법?

hot-time 2020. 5. 11. 08:04
반응형

Swift에서 ViewController를 해제하는 방법?


호출하여 ViewController를 신속하게 해제하려고합니다 dismissViewController.IBAction

  @IBAction func cancel(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
}

@IBAction func done(sender: AnyObject) {
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
}

segue의 무작위 이미지

콘솔 출력에서 ​​println 메시지를 볼 수 있지만 ViewController는 결코 닫히지 않습니다. 무엇이 문제 일 수 있습니까?


당신이 이미지에서 푸시를 사용하여 ViewController를 제시 한 것처럼 보입니다.

dismissViewControllerAnimated모달을 사용하여 발표 가까운 ViewControllers에 사용된다

스위프트 2

navigationController.popViewControllerAnimated(true)

스위프트 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

귀하의 문제에 대한 해결책이 있습니다. 모달을 사용하여 뷰를 표시하는 경우이 코드를 사용하여 뷰 컨트롤러를 닫으십시오.

스위프트 3 :

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

또는

"push"segue를 사용하여보기를 제시하면

self.navigationController?.popViewController(animated: true)

이 작업을 수행하면 콘솔에 println 메시지가 표시되지 않을 수 있습니다.

@IBAction func cancel(sender: AnyObject) {
  if(self.presentingViewController){
    self.dismissViewControllerAnimated(false, completion: nil)
    println("cancel")
   }
}

@IBAction func done(sender: AnyObject) {
  if(self.presentingViewController){
    self.dismissViewControllerAnimated(false, completion: nil)
    println("done")
  }    
}

  1. 해제하려는보기를 NavigationController에 포함
  2. "완료"를 식별자로 사용하여 BarButton 추가
  3. 완료 버튼이 선택된 상태에서 어시스턴트 편집기를 호출하십시오.
  4. create an IBAction for this button
  5. add this line into the brackets:

    self.dismissViewControllerAnimated(true, completion: nil)
    

In Swift 3.0 to 4.0 it's as easy as typing this into your function:

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

Or if you're in a navigation controller you can "pop" it:

self.navigationController?.popViewController(animated: true)

Use:

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

instead of:

self.navigationController.dismissViewControllerAnimated(true, completion: nil)

If you presenting a controller without a Navigation Controller, you can call the following code from a method of the presented controller.

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

If your ViewController is presented modally, optional presentingViewController will be not nil and the code will be executed.


Based on my experience, I add a method to dismiss me as extension to UIViewController:

extension UIViewController {
    func dismissMe(animated: Bool, completion: (()->())?) {
        var count = 0
        if let c = self.navigationController?.viewControllers.count {
            count = c
        }
        if count > 1 {
            self.navigationController?.popViewController(animated: animated)
            if let handler = completion {
                handler()
            }
        } else {
            dismiss(animated: animated, completion: completion)
        }
    }
}

Then I call this method to dismiss view controller in any UIViewController subclass. For example, in cancel action:

class MyViewController: UIViewController {
   ...
   @IBAction func cancel(sender: AnyObject) {
     dismissMe(animated: true, completion: nil)
   }
   ...
}

From Apple documentations:

The presenting view controller is responsible for dismissing the view controller it presented

Thus, it is a bad practise to just invoke the dismiss method from it self.

What you should do if you're presenting it modal is:

presentingViewController?.dismiss(animated: true, completion: nil)

Don't create any segue from Cancel or Done to other VC and only write this code your buttons @IBAction

@IBAction func cancel(sender: AnyObject) {
    dismiss(animated: false, completion: nil)
}

Here is the one way to dismiss present view controller and move back to previous view controller. You can do this through Storyboard only.

  1. Open Storyboard
  2. Right click on Cancel button and drag it to previous view controller, where you want to move back to previous controller
  3. Now release the right click and you can see some actions which performs on cancel button
  4. Now choose "popover present" option from list
  5. Now you can dismiss your current view by click on cancel button

Please try this, It's working with me.

Second Way - Use - navigationController.popViewControllerAnimated(true)

Best luck..


For reference, be aware that you might be dismissing the wrong view controller. For example, if you have an alert box or modal showing on top of another modal. (You could have a Twitter post alert showing on top of your current modal alert, for example). In this case, you need to call dismiss twice, or use an unwind segue.


If you are presenting a ViewController modally, and want to go back to the root ViewController, take care to dismiss this modally presented ViewController before you go back to the root ViewController otherwise this ViewController will not be removed from Memory and cause Memory leaks.


In Swift 3.0

If you want to dismiss a presented view controller

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

In Swift 4.1 and Xcode 9.4.1

If you use pushViewController to present new view controller, use this

self.navigationController?.popViewController(animated: false)

This code written in button action to dismiss

  @IBAction func cancel(sender: AnyObject) {
   dismiss(animated: true, completion: nil)
  }

Try this:

@IBAction func close() {
  dismiss(animated: true, completion: nil)
}

참고 URL : https://stackoverflow.com/questions/24668818/how-to-dismiss-viewcontroller-in-swift

반응형