2013-03-11 4 views
0

저는 iOS 프로그래밍을 처음 접했습니다. 나는 다른 모습의 인물 사진과 가로 방향의 인물이 필요한 곳에 앱을 개발 중입니다. 보기가 다르더라도 단추와 다른 컨트롤은 두보기에서 모두 동일합니다. 그래서 두보기 모두보기 컨트롤러에 단추/레이블에서 동일한 연결을 유지하고 싶습니다. 그래서 내가보기 컨트롤러에서 레이블의 값을 변경하면, 내가 제시하고있는 뷰와 상관없이 뷰에서 변경되어야합니다.단일보기 컨트롤러에 대한 두 개의보기

이제 내가 직면하고있는 문제는 이야기 게시판의 항목에 두 가지 다른 루트보기를 추가 할 수 없다는 것입니다. 주보기에 두 개의 다른 하위보기를 추가하려고했습니다. 그런 식으로 뷰를 만들 수 있지만 두 뷰의 단추를 단일 연결에 연결할 수는 없습니다. 예 :

@property (strong, nonatomic) IBOutlet UIButton *buttonNext;  

은 하나의보기에서만 다음 버튼에 연결할 수 있습니다.

이 문제를 해결하기 위해 스토리 보드에 두 가지 다른보기를 만들었으며 두보기보기 컨트롤러를 모두 추가했습니다. 그런 다음 각보기의 SEGUE을 만들어 :

@property (strong, nonatomic) IBOutlet UIView *portraitView; 
@property (strong, nonatomic) IBOutlet UIView *landscapeView; 

을 그리고 내보기 컨트롤러에서 내가 추가 한 :

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
[self adjustViewsForOrientation:toInterfaceOrientation]; 
} 

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
//NSLog(self.view.restorationIdentifier); 
if (orientation == UIInterfaceOrientationMaskLandscape || orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
{ 
    if(self.landscapeView == nil) 
    { 
     self.landscapeView =[[UIView alloc] init]; 
    } 
    self.view = landscapeView; 
} 
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 
    self.view = portraitView; 
} 
} 

문제는 두 번째보기가 제대로로드되지 않는 것입니다. 처음에는로드되지 않습니다. 코드에서 뷰를 초기화 할 때도 뷰 내부에서 하위 뷰/컨트롤을 초기화하지 않습니다. 나는 지난 3 일 동안 이것으로 붙어있다. 어떤 도움이라도 대단히 감사 할 것입니다.

편집 1 : 여기에 샘플 코드를 추가 한

: U는 하나의보기에 이것을 시도 할 수 있습니다 http://cl.ly/2z3f3E290f0R

답변

3

: viewDidLoad 방법에서

첫째, 장치 오리엔테이션

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

둘째

방법 당신은 'X'& 당신이 원하는대로 'Y'를 취할 수 있으며, 여기에

-(void)orientationChanged { 

    //Check for Portrait Mode 
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     x=25; y=30; 
    } 
    //Lanscape mode 
    else { 
     x=50; y=40; 
    } 

    [button setFrame:CGRectMake(x, y, 80, 80)]; 
} 

을 OrientationChanged.

이 메서드는 장치가 회전 될 때마다 항상 호출합니다.

잘하면, 도움이 될 것입니다.

감사합니다.

+0

감사를 사용해보십시오. 하지만 개체를 ​​크기 조정/다시 배치하는 대신 두 가지 다른보기를 사용하고 있습니다.그렇지 않으면 UI가 풍부하기 때문에 모든 화면에서이 변경을 수행하는 것이 매우 지루할 것입니다. – snowcold

+0

필요에 따라보기를 숨기거나 표시 할 수 있습니다. –

+0

하지만 앞에서 말한 것처럼 동일한 객체 (예 : 버튼)를 사용해야하는 경우 동일한 객체에 대해 여러 연결을 사용하여 관리하는 방법은 무엇입니까? 그런 다음 컨트롤의 크기를 조정해야합니다. –

0

는 응답이

UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation]; 
     if (newOrientation != UIDeviceOrientationUnknown && newOrientation != UIDeviceOrientationFaceUp && newOrientation != UIDeviceOrientationFaceDown) 
     { 
      if(orientation != newOrientation) 
      { 
       //ORIENTATION HAS CHANGED. Do orientation logic 
       if(
        (newOrientation == UIDeviceOrientationLandscapeLeft || newOrientation == UIDeviceOrientationLandscapeRight)&&(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)) 
       { 
        NSLog(@"Changed orientation to Landscape"); 
        // Clear the current view and insert the orientation-specific view 
        [self clearCurrentView]; 
        [self.view insertSubview:titleLandscapeView atIndex:0]; 
       } 
       else if (
         (newOrientation == UIDeviceOrientationPortrait || newOrientation == UIDeviceOrientationPortraitUpsideDown)&&(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) 
       { 
        NSLog(@"Changed orientation to Portrait"); 
        // Clear the current view and insert the orientation-specific view 
        [self clearCurrentView]; 
        [self.view insertSubview:titlePortraitView atIndex:0]; 
       } 
       orientation = newOrientation; 
      } 

-(void) clearCurrentView{ 
     if (titleLandscapeView.superview){ 
      NSLog(@"clear titleLandscapeView"); 
      [titleLandscapeView removeFromSuperview]; 
     } else if (titlePortraitView.superview){ 
      NSLog(@"clear titlePortraitView"); 
      [titlePortraitView removeFromSuperview]; 
     } else 
     { 
      NSLog(@"Neither titleLandscapeView nor titlePortraitView"); 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. 나는 여전히 같은 문제를 겪고있다. 그것은 빈 초상화보기를로드하는 것입니다 – snowcold

+0

u는 주어진 iboulets보기를 가지고 있고 언제 하위보기를 추가하는지는 어떤 방법을 의미합니까 – Dhara

+0

또한 self.view 내부에 두 개의보기를 추가해야합니다. – Dhara

관련 문제