iOS-UITABarViewController 요약

6737 단어
1. UItabBarController의 첫인상
UItabBarController는 UINavigationController와 유사하다. UItabBarController는 여러 개의 컨트롤러를 쉽게 관리하고 컨트롤러 간의 전환을 쉽게 완성할 수 있다. 전형적인 예는 QQ, 위챗 등이다.하지만.전자는 관리하는 보기가 계속 존재하지만 후자는 팝 후에 메모리를 없애고 방출한다.
주의: UITABarController는 보통 프로그램 전체의 루트 ViewController이며, 다른 container view Controller에 추가할 수 없습니다.
2. UITABarController의 사용:
1. 사용 단계:
(1) UITABarController 초기화
(2) UIWindow의 rootViewController를 UItabBarController로 설정
(3) 상응하는 하위 컨트롤러 만들기 (viewcontroller)
(4) UITABarController에 하위 컨트롤러 추가
2. 코드 예
새 빈 UITABarController, 컨트롤러에서 인코딩
-(void) viewDidLoad {​

    MapViewController *v1 = [MapViewControllernew];

    v1.tabBarItem.image = [UIImageimageNamed:@"icon_map"];

    v1.tabBarItem.title = @"  ";

    v1.view.backgroundColor = [UIColororangeColor];

    CycleViewController *v2 = [CycleViewControllernew];

    v2.tabBarItem.image = [UIImageimageNamed:@"icon_cycle"];

    v2.tabBarItem.title = @"  ";

    v2.view.backgroundColor = [UIColorgreenColor];

    OtherViewController *v3 = [OtherViewControllernew];

    v3.tabBarItem.image = [UIImageimageNamed:@"icon_other"];

    v3.tabBarItem.title = @"  ";

    v3.view.backgroundColor = [UIColoryellowColor];

    MineViewController *v4 = [MineViewControllernew];

    v4.tabBarItem.image = [UIImageimageNamed:@"icon_user"];

    v4.tabBarItem.title = @"  ";

    v4.view.backgroundColor = [UIColorredColor];

UINavigationController *n1 = [[UINavigationControlleralloc] initWithRootViewController:v1];

​UINavigationController *n2 = [[UINavigationControlleralloc] initWithRootViewController:v2];

UINavigationController *n3 = [[UINavigationControlleralloc] initWithRootViewController:v3];

UINavigationController *n4 = [[UINavigationControlleralloc] initWithRootViewController:v4];

NSArray *array = @[n1,n2,n3,n4];

self.viewControllers = array;

}


이렇게 하면 네 개의 네비게이션 컨트롤러가 있는tabBarController를 만들 수 있습니다. Appdelegate에서 이 UITbarBarcontroller 컨트롤러를 보기 컨트롤러로 설정하기만 하면 됩니다.
self.window.rootViewController = [[UITbarBarcontroller alloc]​  init];

중요 설명
1、UITabBar ​
아래의 도구막대를 UITA Bar라고 하는데 UITA Bar Controller에 N 서브 컨트롤러가 있다면 UITA Bar 내부에 N 개의 UITA Bar Button이 서브 컨트롤러로 대응할 것이다(단 맨 밑에 최대 5개가 표시되고 많으면 마지막 칸에 숨겨져 있어 클릭해서 볼 수 있다).
주의: UITA Bar Button은 UITA Bar에서 차지하는 위치가 균등하고 UITA Bar의 높이는 49이다.
2、UITabBarButton
UItabBarButton은 어떤 내용을 나타낼지 하위 컨트롤러의tabBarItem 속성에 따라 결정합니다
c1.tabBarItem.title=@"  "; 
c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];​

UITABarItem 은 다음과 같은 속성으로 UITABarButton 컨텐츠에 영향을 미칩니다.
title (제목), 이미지 (아이콘),selectImage (선택한 아이콘),badgeValue (알림 숫자)
3. UITABarController에 하위 컨트롤러를 추가할 수 있는 두 가지 방법이 있다
(1)[tb addChildViewController:c1];
(2)tb.viewControllers=@[c1,c2,c3,c4];
주의: 전시된 순서와 추가된 순서가 일치하며 내비게이션 컨트롤러와 달리 눈앞에 보이는 것은 첫 번째로 추가된 컨트롤러에 대응하는 View입니다.
​3、UITabBarItem ​-
  • UITA Bar Item UITA Bar에 표시된 모든 Tab은 View Controller에 대응하고 우리는view controller를 설정할 수 있다.tabBarItem 속성을 사용하여 tabbar에 해당하는 tab 표시 내용을 변경합니다.
  • 그렇지 않으면 시스템은viewController의 titler에 따라 자동으로 생성됩니다. 이tabBarItem은 텍스트만 표시하고 그림은 없지만 그림의 위치는 미리 남깁니다.
  • UItabBarItem을 만들 때 지정한 그림과 대응하는 텍스트 설명을 표시할 수 있습니다.
  • 물론 setFinishedSelectedImage: withFinishedUnselectedImage: 방법은 선택 상태와 비행 선택 상태에 서로 다른 그림을 지정할 수 있습니다.​
  • UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];  
    ​[item setFinishedSelectedImage:[UIImage imageNamed:@"second"] withFinishedUnselectedImage:[UIImage imageNamed:@"first"]];  
    viewController2.tabBarItem = item; 
    

    4. Change SelectedViewcontroller(현재 선택한 컨트롤러 변경)
    UITABarController에 현재 표시된viewController를 변경하려면 다음 두 가지 방법을 사용합니다.
    1. selectedIndex 속성
  • 이 속성을 통해 현재 선택된viewController를 얻을 수 있습니다. 이 속성을 설정하면viewControllers에 대응하는 index의viewController를 표시할 수 있습니다.
  • 현재 MoreViewController가 선택되어 있으면 이 속성이 가져온 값은 NSNotFound이며, 이 속성을 통해 MoreViewController를 선택할 수 없습니다.index가viewControllers의 범위를 초과하도록 설정하면 무시됩니다.

  • 2. selectedViewController 속성
  • 이 속성을 통해 현재 보이는viewController를 얻을 수 있습니다. 이 속성을 설정하면 현재 선택된viewController를 설정하고selectedIndex를 업데이트할 수 있습니다.
  • 이 속성에 값을 부여하면tabBarController.moreNavigationController는 moreViewController를 선택할 수 있습니다.

  • 3. viewControllers 속성
    view Controllers 속성을 설정하면 현재 선택된view Controller에 영향을 줍니다. 이 속성을 설정할 때 UITA Bar Controller는 오래된 view Controller를 모두 비우고 새view Controller를 배치합니다. 마지막으로 표시된view Controller를 다시 선택하십시오. 이view Controller가 존재하지 않으면 index와selected Index가 같은view Controller를 선택하십시오. 이 index가 잘못되면첫 번째viewController가 기본적으로 선택됩니다.
    5.moreNavigationController
  • UItabBar에 최대 5개의 탭을 표시할 수 있습니다. UItabBarController에 추가된viewController가 5개를 넘으면 마지막 탭은 자동으로 more으로 바뀌고 설정된viewControlles 순서에 따라 앞의 4개의viewController의tabBarItem을 표시하며 뒤의 탭BarItem은 더 이상 표시되지 않습니다.more를 눌렀을 때 표준navigation View Controller가 팝업됩니다. 다른 보이지 않는view Controller가 있고 편집 단추가 있습니다. 이 단추를 누르면 아이팟 프로그램에서 설정한tabBar와 같은 편집 인터페이스에 들어갈 수 있습니다.
  • 편집 인터페이스에서 기본적으로 모든viewController는 편집할 수 있습니다. UITABarController의customizable ViewControllers 속성을 설정하여viewControllers의 하위 집합을 지정할 수 있습니다. 즉, 일부viewController만 tabBar에 표시할 수 있도록 허용합니다.그러나 이 부분에서 주의해야 할 문제는 UITA Bar Controller의view Controllers 속성이 변할 때마다customizable View Controllers는view Controllers와 일치하도록 자동으로 설정된다는 것이다. 즉, 기본적인 모든view Controller는 편집할 수 있는 것이다. 만약에 우리가 시종일관 편집할 수 있는 일부분만을 제한하려면view Controlles가 변할 때마다customizable ViewControllers를 다시 설정합니다.

  • 6. UITABarController의 Rotation
  • UItabBarController는 기본적으로 세로 화면만 지원합니다. 장치의 방향이 바뀔 때viewControllers에 포함된 모든 ViewController를 조회합니다. 모든viewController가 이 방향을 지원할 때만 UItabBarController가 회전합니다. 그렇지 않으면 묵인된 세로 방향이 발생합니다.
  • UItabBarController가 회전을 지원하고 회전이 발생할 때 현재 보이는viewController만 회전 메시지를 받을 수 있음을 주의해야 한다.

  • 7. UItabBarControllerDelegate
  • 이 두 가지 방법은 제가 예전에 프로젝트에서 앱을 열 때마다 백엔드 데이터에 따라 지정한tabbar의 이미지를 바꾸고 클릭한 후에 이미지를 바꿉니다.
  • - tabBarController:shouldSelectViewController:
    - tabBarController:didSelectViewController:
    
  • 아래의 세 가지 방법은moreViewController에서viewcontroller에 대한 편집 조작
  • 을 모니터링하는 데 주로 사용된다.
    - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;
    
    - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
    
    - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;
    

    좋은 웹페이지 즐겨찾기