탭 표시줄 탭 보기로 프로그래밍 방식으로 전환하시겠습니까?
예를 들어, 나에게UIButton
내 아이폰 앱의 한 탭 보기에서, 그리고 나는 그것이 그것의 탭 바에서 다른 탭을 열기를 원합니다.TabBarController
이를 위해 코드를 어떻게 작성해야 합니까?
기존 뷰를 언로드하고 특정 탭 뷰를 로드한다고 가정하는데 코드를 정확히 작성하는 방법을 모르겠습니다.
Swift 또는 Objective-C에서 이 코드를 시도합니다.
스위프트
self.tabBarController.selectedIndex = 1
목표-C
[self.tabBarController setSelectedIndex:1];
탭은 0부터 색인화됩니다.그래서 다음 코드 스니펫은 작동합니다.
tabBarController = [[UITabBarController alloc] init];
.
.
.
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:4];
바의 다섯 번째 탭으로 이동합니다.
제 의견은selectedIndex
또는 사용objectAtIndex
탭을 전환하는 가장 좋은 방법은 아닙니다.탭을 다시 정렬하면 하드 코딩된 인덱스 선택이 이전 앱 동작을 방해할 수 있습니다.
전환할 뷰 컨트롤러의 개체 참조가 있는 경우 다음 작업을 수행할 수 있습니다.
tabBarController.selectedViewController = myViewController
물론 당신은 확인해야 합니다, 그것.myViewController
정말로 의 목록에 있습니다.tabBarController.viewControllers
.
간단히 설정할 수 있습니다.selectedIndex
사용자가 탭 버튼을 누른 것처럼 보기가 변경됩니다.
디스코 S2가 제안한 것을 시도해봤는데, 가까웠지만 결국 이것이 저에게 효과가 있었습니다.다른 탭 내에서 작업을 완료한 후 호출되었습니다.
for (UINavigationController *controller in self.tabBarController.viewControllers)
{
if ([controller isKindOfClass:[MyViewController class]])
{
[self.tabBarController setSelectedViewController:controller];
break;
}
}
Stuart Clark의 솔루션처럼 Swift 3의 경우:
func setTab<T>(_ myClass: T.Type) {
var i: Int = 0
if let controllers = self.tabBarController?.viewControllers {
for controller in controllers {
if let nav = controller as? UINavigationController, nav.topViewController is T {
break
}
i = i+1
}
}
self.tabBarController?.selectedIndex = i
}
다음과 같이 사용합니다.
setTab(MyViewController.self)
내 탭 컨트롤러는 탐색 컨트롤러 뒤에 있는 보기 컨트롤러에 연결됩니다.내비게이션 컨트롤러가 없으면 다음과 같습니다.
if let controller is T {
제 문제는 조금 다릅니다. 첫 번째 탭 바의 하나의 childViewController에서 두 번째 탭 바의 홈 뷰Controller로 전환해야 합니다.나는 위층에 제공된 솔루션을 사용합니다.
tabBarController.selectedIndex = 2
그러나 두 번째 탭바의 홈 페이지로 전환했을 때 내용이 보이지 않습니다.디버깅을 하고, DidApear를 보고, WillApear를 보고, DidLoad를 보고, 아무 것도 호출되지 않습니다.내 해결책은 다음 코드를 UITabBarController에 추가하는 것입니다.
override var shouldAutomaticallyForwardAppearanceMethods: Bool
{
return true
}
탭을 이동하는 경우 몇 가지 코드가 있습니다.
for ( UINavigationController *controller in self.tabBarController.viewControllers ) {
if ( [[controller.childViewControllers objectAtIndex:0] isKindOfClass:[MyViewController class]]) {
[self.tabBarController setSelectedViewController:controller];
break;
}
}
IB를 연결하는 방법에 덜 의존하는 강력한 솔루션을 위해 만들어졌다고 생각했기 때문에 인덱스가 아닌 클래스별로 표시되는 탭을 지정할 수 있기를 원했습니다.Disco나 Joped의 해결책을 찾지 못했기 때문에 다음 방법을 만들었습니다.
-(void)setTab:(Class)class{
int i = 0;
for (UINavigationController *controller in self.tabBarContontroller.viewControllers){
if ([controller isKindOfClass:class]){
break;
}
i++;
}
self.tabBarContontroller.selectedIndex = i;
}
당신은 그것을 이렇게 부릅니다.
[self setTab:[YourClass class]];
이것이 누군가에게 도움이 되기를 바랍니다.
import UIKit
class TabbarViewController: UITabBarController,UITabBarControllerDelegate {
//MARK:- View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//Tabbar delegate method
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
yourView.popToRootViewController(animated:false)
}
}
사용 위치AppDelegate.m
파일 이름:
(void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(@"Selected index: %d", tabBarController.selectedIndex);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
NSUInteger selectedIndex = tabBarController.selectedIndex;
switch (selectedIndex) {
case 0:
NSLog(@"click me %u",self.tabBarController.selectedIndex);
break;
case 1:
NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
break;
default:
break;
}
}
Stuart Clark의 솔루션과 마찬가지로 Swift 3의 경우와 올바른 탭을 찾기 위해 복원 식별자를 사용합니다.
private func setTabById(id: String) {
var i: Int = 0
if let controllers = self.tabBarController?.viewControllers {
for controller in controllers {
if let nav = controller as? UINavigationController, nav.topViewController?.restorationIdentifier == id {
break
}
i = i+1
}
}
self.tabBarController?.selectedIndex = i
}
다음과 같이 사용합니다("인간" 및 "로봇"도 특정 보기 컨트롤러와 해당 복원 ID에 대해 스토리보드에 설정해야 합니다). 또는 스토리보드 ID를 사용하고 "스토리보드 ID를 복원 ID로 사용"을 선택합니다.
struct Tabs {
static let Humans = "Humans"
static let Robots = "Robots"
}
setTabById(id: Tabs.Robots)
내 탭 컨트롤러는 탐색 컨트롤러 뒤에 있는 보기 컨트롤러에 연결됩니다.내비게이션 컨트롤러가 없으면 다음과 같습니다.
if controller.restorationIdentifier == id {
언급URL : https://stackoverflow.com/questions/5413538/switching-to-a-tabbar-tab-view-programmatically
'sourcecode' 카테고리의 다른 글
루비의 해시 값을 기준으로 해시 배열 내에서 검색하려면 어떻게 해야 합니까? (0) | 2023.05.28 |
---|---|
Swift에서 한 뷰 컨트롤러의 방향을 세로 모드로만 잠그는 방법 (0) | 2023.05.28 |
Azure 웹 사이트 301 리디렉션 - 어디에 두어야 합니까? (0) | 2023.05.28 |
CentOS 8.x/7.x/6에 Git의 최신 버전을 설치하는 방법.x (0) | 2023.05.28 |
.git는 커밋 후 무시합니다. (0) | 2023.05.28 |