2011-03-14 3 views
0

그래, 조금만 붙어서 누군가가 조언을 빌려 줄 수 있습니다.서브 클래 싱 된 UITabBarController에서 생성 된 액세스 버튼

UITabBarController를 서브 클래 싱 한 후 CustomTabBarController 안에 viewDidLoad이 호출 될 때마다 탭 막대를 오버레이하는 사용자 정의 버튼을 만듭니다.

이것은 어떤 행동에도 연계되어 있지 않다는 점을 제외하고는 훌륭하게 작동합니다.

내가 뭘하고 싶은데 그 버튼을 누르면 UIModalViewController가 표시됩니다. 이제, 바람직하게는 서브 클래 싱 된 서브 클래스 CustomTabBarController에서이 호출을하지 않고 탭과 연관된 뷰 컨트롤러 (rootViewController) 중 하나에서 호출하는 것이 바람직합니다.

다른 사람이 나를 어떻게 안내 할 수 있습니까? IE, 한 클래스에서 버튼을 인스턴스화하고 해당 버튼이 다른 클래스 내의 액션에 응답하는 방법.

NSNotificationCenter을 사용해야합니까, 응답자를 위임해야합니까? 좋은 예가있을 것입니다 :)

답변

0

UITabBarController에있는 모든보기 컨트롤러를 viewControllers 배열로 참조 할 수 있습니다. selectedViewController으로 현재 선택된보기에 대한보기 컨트롤러를 쉽게 얻을 수 있습니다.

동작을 염두에두고 CustomTabBarController 작업은 이러한보기 컨트롤러에서 메서드를 호출 할 수 있습니다. 적절한 뷰 컨트롤러에 메서드를 추가하여 UIModalViewController을 표시하면됩니다.

2

당신이 원하는 것을 얻기위한 몇 가지 접근 방법이 있습니다. 그래서 난 그냥 방법 buttonAction:를 호출, 난 당신이 버튼을하고있을 것입니다하는지 모르겠

// CustomTabBarController.h 
@protocol CustomTabBarControllerDelegate 
- (void)buttonAction:(id)sender; 
@end 

@interface CustomTabBarController : UITabBarController { 
    id<CustomTabBarControllerDelegate> customDelegate; 
} 
@property(nonatomic, assign) id<CustomTabBarControllerDelegate> customDelegate; 
@end 

// CustomTabBarController.m 
@interface CustomTabBarController() 
- (void)buttonAction:(id)sender; 
@end 

@implementation CustomTabBarController 
@synthesize customDelegate; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    // Configuration of button with title and style is left out 
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 
} 
- (void)buttonAction:(id)sender { 
    [self.customDelegate buttonAction:sender]; 
} 
@end 

: 나는 보통 가지고 접근 방식은 내가 같은 것을 할 것입니다. UITabBarController은 이미 delegate이라는 속성을 가지고 있으므로 대표자는 customDelegate입니다. 위 작업을하기 위해서해야 할 일은 루트 뷰 컨트롤러 (또는 버튼 동작을 처리하고자하는 컨트롤러)에 다음 행을 추가하는 것입니다.

customTabBar.customDelegate = self; 

물론 프로토콜을 구현해야합니다.

하나는이 같은 목표를 대리자를 사용하여 그냥 설정하지 상상할 수 :

[button addTarget:self.rootViewController action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 

위의 코드는 customTabBarControllerrootViewController 속성이 있다고 가정하고이 설정되어 있는지. 그것은 일반적인 접근 방식으로

- (void)buttonAction:(id)sender; 

내가 위임 방식을 선호하지만 나중에 접근 방식은 또한 작동합니다 : 또한 그것은 루트 뷰 컨트롤러는 다음과 같은 방법이 있다고 가정합니다. NSNotificationCenter을 사용하는 것도 옵션이지만 개인적으로는 필요하지 않을 때 알림을 보내는 팬이 아닙니다. 일반적으로 여러 객체가 이벤트에 응답해야하는 경우에만 알림을 사용합니다.

관련 문제