뷰 응용 프로그램/뷰 컨트롤러가 작동하는 방식과 스토리 보드 또는 펜촉없이 프로그래밍 방식으로 탐색하는 방법을 이해하는 테스트 응용 프로그램을 만들었습니다.뷰를 변경하면 뷰가 창 계층 구조에없는 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
View1에서 단추를 누를 때 오류가 발생하면 해당 단추의 조치 방법에있는 코드를 표시해야합니다. – rdelmar
코드를 추가했습니다. 두보기는 다른보기가 다른보기를 표시하기 위해 알림을 전송한다는 점을 제외하면 동일합니다. – thegalah