2015-01-30 5 views
0

뷰 응용 프로그램/뷰 컨트롤러가 작동하는 방식과 스토리 보드 또는 펜촉없이 프로그래밍 방식으로 탐색하는 방법을 이해하는 테스트 응용 프로그램을 만들었습니다.뷰를 변경하면 뷰가 창 계층 구조에없는 vc1을 vc1에 계속 표시하려고 시도합니다.

나는 기본적으로 AppDelegate에 설정된 Viewcontroller라는 rootViewController을 설정했습니다.

이보기는의 알림을 호출하여 view2를 표시하는 uibutton을 포함하는 시작시 view1을 제공합니다. 나는이 작업을 수행하지만 때 경고가 계속 :

Warning: Attempt to present <View1: 0x7be66c80> on <ViewController: 0x7be62980> whose view is not in the window hierarchy! 

다음과 같이 제어의 ViewController에 대한 나의 코드는 다음과 같습니다

 

    #import "ViewController.h" 
    #import "View1.h" 
    #import "View2.h" 
    @interface ViewController() 

    @end 

    @implementation ViewController 
    -(void) loadView{ 
     [super loadView]; 
    } 
    -(void)viewDidAppear:(BOOL)animated{ 
     NSLog(@"View appeared"); 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"view1" object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(view1:) name:@"view1" object:nil]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"view2" object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(view2:) name:@"view2" object:nil]; 
     [self showView1]; 
    } 
    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 
    -(void)view1:(NSNotification*) notification{ 
     NSLog(@"Showing view1"); 
     [self dismissViewControllerAnimated:YES completion:nil]; 
     [self showView1]; 
    } 
    -(void)showView1{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.view.window.rootViewController presentViewController:[[View1 alloc]init] animated:YES completion:nil]; 
     }); 
    } 
    -(void)view2:(NSNotification*) notification{ 
     NSLog(@"Showing view2"); 
     [self dismissViewControllerAnimated:YES completion:nil]; 
     [self showView2]; 
    } 
    -(void)showView2{ 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self presentViewController:[[View1 alloc]init] animated:YES completion:nil]; 
     }); 
    } 

@end 

편집 : 내가 어떤 오류 또는 문제없이 처음에 그 뷰 1 표시를 언급하는 것을 잊었다 .

편집 : 뷰 1와 뷰 2의 코드가 서로 다른 통지 보낼 것을 제외하고는 정확히 동일합니다 : 당신이 즉시 showView2을 (전화, 애니메이션 뷰 1을 기각하고 있기 때문에

 

    #import "View1.h" 

    @interface View1() 

    @end 

    @implementation View1 

    - (void)loadView { 
     [super loadView]; 
     self.view.frame = [[UIApplication sharedApplication].keyWindow bounds]; 
     // Do view setup here. 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button addTarget:self 
        action:@selector(touchedLogin:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     [button setTitle:@"Login" forState:UIControlStateNormal]; 

     button.layer.cornerRadius=1; 
     button.frame = CGRectMake(self.view.bounds.size.width/2-100, self.view.bounds.size.height-200, 200, 60.0); 
     [button setTitleEdgeInsets:UIEdgeInsetsMake(5.0, 0.0, 0.0, 0.0)]; 

     //[_window setBackgroundColor:[UIColor whiteColor]]; 
     [self.view addSubview:button]; 
    } 
    -(void)touchedLogin:(id*)sender{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"view2" object:nil]; 
    } 

    @end 

+0

View1에서 단추를 누를 때 오류가 발생하면 해당 단추의 조치 방법에있는 코드를 표시해야합니다. – rdelmar

+0

코드를 추가했습니다. 두보기는 다른보기가 다른보기를 표시하기 위해 알림을 전송한다는 점을 제외하면 동일합니다. – thegalah

답변

0

당신은이 오류가있는 사실을 보여줍니다 View1, 그 방법에서 [View1 alloc] init을 가지고 있기 때문에). 애니메이션에는 어느 정도의 시간이 걸리기 때문에 View1이 ViewController가 아닌 화면에있는 동안 View1을 표시하려고합니다. 일반적으로 해고와 발표를하는 것이 좋은 아이디어는 아닙니다. 나는 애니메이션없이 해고를한다면 효과가있을 것이라고 생각한다.

+0

모든 애니메이션을 제거하려고 시도했지만 오류가 계속 발생합니다. 나는 해고와 발표에 파견 비동기를 사용하여 아무런 성공도 시도하지 않았다. – thegalah

+0

@thegalah, 그래, 그게 효과가 있을지 모르겠다. 모든 것이 메인 스레드에 있기 때문에 dispatch_async를 사용하는 것은 코드에서 불필요합니다. 당신의 모든 접근 방식이 좋지 않습니다. 해고 직후에 발표를하지 마십시오. View1에서 View2로 이동하려면 View1에 View2를 표시하십시오. 또한 당신과 같은 알림을 사용하는 것이 최선의 방법이 아닙니다. 컨트롤러가 여러 개있을 때 사용하는 것이 가장 좋습니다. 여기에있는 것뿐만 아니라 몇 가지 작업에 대한 알림을 받아야합니다. – rdelmar

+0

바라는대로 뷰간에 이동하는 방법은 무엇입니까? 나는 ios haha ​​ – thegalah