문제

누구든지 먼저 모달 뷰 컨트롤러를 제시하는 데 사용할 수있는 예제 코드를 나에게 줄 수 있습니까? 이것이 제가 시도한 것입니다.

    NSLog(@"%@", blue.modalViewController);
    [blue presentModalViewController:red animated:YES];
    NSLog(@"%@", blue.modalViewController);
    [blue dismissModalViewControllerAnimated:YES];
    NSLog(@"%@", blue.modalViewController);
이 코드는 ViewDidLoad ( "파란색"및 "빨간색"은 UIViewController의 서브 클래스입니다). 나는 빨간보기를 보여준 다음 약간의 애니메이션으로 즉시 숨길 것으로 기대합니다. 그러나이 코드는 모달보기만을 제시하며이를 무시하지 않습니다. 아이디어가 있습니까? 첫 번째 로그는 "NULL"을 보여주고 다른 두 로그가 표시됩니다.u003CRedViewController: 0x3d21bf0>
또 다른 요점은,이 코드를 ApplicationDidFinishLaunching에 넣으면 : 빨간보기가 전혀 나타나지 않고 모든 로그가 "null"이됩니다.

도움이 되었습니까?

해결책

우선, 해당 코드를 ApplicationDidFinishLaunching에 넣을 때 인터페이스 빌더에서 인스턴스화 된 컨트롤러가 아직 응용 프로그램에 연결되어 있지 않은 경우 (따라서 "빨간색"및 "파란색"이 여전히 있습니다. nil).

하지만 초기 질문에 답하기 위해, 당신이 잘못하고있는 일은 당신이 dismissModalViewControllerAnimated: 잘못된 컨트롤러에! 다음과 같아야합니다.

[blue presentModalViewController:red animated:YES];
[red dismissModalViewControllerAnimated:YES];

일반적으로 "빨간색"컨트롤러는 어느 시점에서 자신을 기각하기로 결정해야합니다 (아마도 "취소"버튼을 클릭 할 때). 그런 다음 "빨간색"컨트롤러가 메소드를 호출 할 수 있습니다. self:

[self dismissModalViewControllerAnimated:YES];

여전히 작동하지 않으면 컨트롤러가 애니메이션 방식으로 제시되었다는 사실과 관련이있을 수 있으므로 제시 직후 컨트롤러를 해제 할 수 없습니다.

다른 팁

Xcode 4.52에서 피곤한 가장 쉬운 방법은 Segue Modal을 사용하여 추가 뷰를 만들고 연결하는 것이 었습니다 (컨트롤 버튼을 하나에서 두 번째보기, Modal을 선택했습니다). 그런 다음 버튼을 두 번째보기 또는 생성 한 모달보기로 드래그하십시오. 이 버튼을 헤더 파일로 제어하고 드래그하고 조치 연결을 사용하십시오. Controller.m 파일에 iBaction이 생성됩니다. 코드에서 버튼 작업 유형을 찾으십시오.

[self dismissViewControllerAnimated:YES completion:nil];

빠른

Swift 3에 대한 업데이트

enter image description here

스토리 보드

각각의 버튼이있는 두 개의보기 컨트롤러를 만듭니다. 두 번째보기 컨트롤러의 경우 클래스 이름을 SecondViewController 그리고 스토리 보드 ID로 secondVC.

암호

ViewController.swift

import UIKit
class ViewController: UIViewController {

    @IBAction func presentButtonTapped(_ sender: UIButton) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let myModalViewController = storyboard.instantiateViewController(withIdentifier: "secondVC")
        myModalViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        myModalViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        self.present(myModalViewController, animated: true, completion: nil)
    }
}

SecondViewController.swift

import UIKit
class SecondViewController: UIViewController {

    @IBAction func dismissButtonTapped(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
}

원천:

현재 modalViewController :

MainViewController *mainViewController=[[MainViewController alloc]init];
[self.navigationController presentModalViewController:mainViewController animated:YES];

DismissModalViewController :

[self dismissModalViewControllerAnimated:YES];

빠른

self.dismissViewControllerAnimated(true, completion: nil)

가장 쉬운 방법은 스토리 보드와 Segue를 사용하는 것입니다.

TabBarController의 FirstViewController (Navigation Controller가 아님)에서 Login UI가있는 LoginViewController로 Segue를 작성하고 "showlogin"이름을 지정하십시오.

사용자가 로그인하거나 세션이 유효한 경우 부울을 반환하는 메소드를 작성하십시오. ItsessionValid라고 부릅니다.

FirstViewController.m에서 다음과 같이 Method ViewDidAppear를 무시합니다.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if([self isSessionValid]==NO){
        [self performSegueWithIdentifier:@"showLogin" sender:self];
    }
}

그런 다음 사용자가 성공적으로 로그인 한 경우 LoginViewController를 기각하거나 팝업하여 탭을 표시하십시오.

100%작동합니다!

도움이되기를 바랍니다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top