2013-08-09 6 views
-1

장치가 회전 할 때 주 창에 추가 된 항목의 UIView 하위 클래스에서 함수를 호출하려고합니다.장치가 회전 할 때마다 UIView 하위 클래스에서 메서드를 호출하려면 어떻게해야합니까?

나는 많은 아이템을 가지고 있으므로 이러한 메소드를 하나씩 호출하고 싶지는 않지만이 아이템과 모든 서브 뷰에 대해 자동으로 호출되기를 원한다.

감사

+0

'NSNotificationcenter' – Exploring

답변

1

는 자동 서브 뷰 동작을 얻으려면, 당신이 호출 할 방법을 추가, UIView 범주를 선언합니다. 그런 다음보기가 장치 회전 알림을 구독하도록하고 알림 처리의 일부로 모든 하위보기에서 동일한 메서드를 호출합니다. 이런 식으로 뭔가 (자동 고침의 혜택없이 입력 또는 컴파일 된, 그래서 그림으로 가져가 아닌 전체 예제 프로그램하십시오) :

@interface UIView (MyAppAdditions) 

- (void)rotationDetected; 

@end 

@implementation UIView (MyAppAdditions) 

- (void)rotationDetected 
{ 

} 

@end 


@implementation MyView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
    } 

    return self; 
} 

- (void)didRotate:(NSNotification *)notification 
{ 
    [self rotationDetected]; 
    [self.subviews makeObjectsPerformSelector:@selector(rotationDetected)]; 
} 

@end 

더 유연한 구현도 회전 알림을뿐만 아니라 가입 할 initWithCoder: 또는 awakeFromNib을 무시할 것 .

관련 문제