IT박스

신속한 presentViewController

itboxs 2020. 11. 6. 07:58
반응형

신속한 presentViewController


프로그래밍 방식으로 iOS Swift 프로젝트에 여러 뷰 컨트롤러가 있습니다. 스토리 보드가 없으며 가능하면 피하고 싶습니다. 다른 viewcontroller.swift파일 로 전환하고 (라고 부를 것입니다 view2.swift) 버튼이 호출하는 함수의 일부가되는 방법이 있습니까?
나는 다음을 시도했다 :

let storyboard: UIStoryboard = UIStoryboard(name: "myTabBarName", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("myVCID") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)

위의 내용은 스토리 보드에서 작동하지만 다른 사람 view2.swift이 호출 되기를 바랍니다 . 할 수 있습니까?


이 시도:

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)

Swift3 사용 :

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

빈 / 검은 화면이 나타나는 사람들을 위해이 코드가 저에게 효과적이었습니다.

    let vc = self.storyboard?.instantiateViewController(withIdentifier: myVCID) as! myVCName
    self.present(vc, animated: true, completion: nil)

"식별자"를 VC로 설정하려면 스토리 보드에서 VC의 ID 검사기로 이동하십시오. '스토리 보드 ID'를 원하는 식별자로 설정하십시오. 아래 이미지를 참조하십시오.


참고로이 질문은 Google의 첫 번째 결과 중 하나이기 때문입니다.

Swift 3의 브레이킹 체인지 :

메서드 presentViewController메서드 로 대체됩니다 present.

이전처럼 사용할 수 있습니다.

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

카메라를 여는 예 :

let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)

Swift 3 및 Swift 4

let vc = self.storyboard?.instantiateViewController(withIdentifier: "idMyViewControllerName") as! MyViewControllerName
self.present(vc, animated: true, completion: nil)

저에게는 두 개의 별도 탐색 컨트롤러에 두 개의 뷰가 있습니다. 위의 조합을 사용해야했습니다.

var vc = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeViewController") as! WelcomeViewController
    var navigationController = UINavigationController(rootViewController: vc)
    self.presentViewController(navigationController, animated: true, completion: nil)

스위프트 3.x

        let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "VC-ID" as! yourViewController
        let navigationVC = UINavigationController(rootViewController: secondVC)
        self.present(navigationVC, animated: true, completion: nil)

Swift 2.1 이상 사용

 let vc = self.storyboard?.instantiateViewControllerWithIdentifier("settingsVC") as! SettingsViewController
 self.presentViewController(vc, animated: true, completion: nil)

enter image description here


이것을 사용하십시오 : nibName을 사용하십시오. 그렇지 않으면 xib의 미리로드 된 뷰가 표시되지 않습니다.

var vc : ViewController = ViewController(nibName: "ViewController", bundle: nil) // 이것을 클래스 이름으로 변경하십시오.

 self.presentViewController(vc, animated: true, completion: nil)

탐색 컨트롤러를 추가하고 두 번째보기 컨트롤러를 rootVC로 설정하여 검은 색 화면을 해결했습니다.

let vc = ViewController()       
var navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil

present()ViewController가 작동하도록 하기 위해 Storyboard에서 ViewController를 인스턴스화 할 필요가 없습니다 . 그것은 hackish 솔루션입니다.

If you see a black/blank screen when presenting a VC, it might be because you're calling present() from viewDidLoad() in the First/RootViewController, but the first View isn't ready yet.

Call present() from viewDidAppear to fix this, i.e.:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let yourVC = YourViewController()
    self.present(yourVC, animated: true, completion: nil)
}

Once any "View" has appeared in your App, you can start calling present() from viewDidLoad().


Using UINavigationController (as suggested in an answer) is another option, but it might be an overkill to solve this issue. You might end up complicating the user flow. Use the UINavigationController based solution only if you want to have a NavigatonBar or want to return to the previous view controller.


You can use below code :

var vc = self.storyboard?.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController;
            vc.mode_Player = 1
            self.presentViewController(vc, animated: true, completion: nil)

Another possibility is that you do not have the XIB included in your Build target (which is what happened to me).

This could happen if you have a different target for Dev, Test & Release Builds (which you should have anyway).


You can use code:

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? secondViewController {
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = vc
}

참고URL : https://stackoverflow.com/questions/24099533/swift-presentviewcontroller

반응형