태그 보관물: swift

swift

Swift 내에서 프로그래밍 방식으로 탭 간 전환

iOS 앱이 시작될 때보기를 다른 탭으로 전환하는 코드를 작성해야합니다 (예를 들어 두 번째 탭은 기본적으로 첫 번째 탭이 아닌 탭으로 표시됨).

저는 Swift를 처음 사용하며 다음을 해결했습니다.

  • 코드는 아마도 첫 번째 탭의 ViewController에있는 override func viewDidLoad () 함수에 있어야합니다.

  • 다음 코드는 두 번째 ViewController를 보여 주지만 맨 아래에 탭 표시 줄이 없습니다 (vcOptions는 두 번째 ViewController 탭 항목입니다.

let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)

대답은 UITabbarController.selectedIndex = 1을 사용하는 데 있다고 생각하지만 이것을 구현하는 방법은 확실하지 않습니다.



답변

귀하의 경우 window rootViewController이다 UITabbarController(대부분의 경우입니다) 당신은에 액세스 할 수 있습니다 tabbardidFinishLaunchingWithOptionsAppDelegate파일.

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    if let tabBarController = self.window!.rootViewController as? UITabBarController {
        tabBarController.selectedIndex = 1
    }

    return true
}

그러면에서 index주어진 (1) 탭이 열립니다 selectedIndex.

당신이 이렇게하면 viewDidLoad당신의 firstViewController, 당신은 플래그 또는 선택된 탭을 추적하는 또 다른 방법으로 관리해야합니다. 최고의 장소에서이 작업을 수행하는 didFinishLaunchingWithOptions사용자의 AppDelegate파일 또는 rootViewController사용자 정의 클래스 viewDidLoad.


답변

1. UITabBarController를 대체하는 새 클래스를 만듭니다. 예 :

class xxx: UITabBarController {
override func viewDidLoad() {
        super.viewDidLoad()
}

2. viewDidLoad () 함수에 다음 코드를 추가합니다.

self.selectedIndex = 1; //set the tab index you want to show here, start from 0

3. Storyboard로 이동하여 Tab Bar Controller의 Custom Class를이 새 클래스로 설정합니다. (사진의 예로서 MyVotes1)


답변

스위프트 3

index 0tabBarController 의 기본 뷰 컨트롤러 ( ) 에이 코드를 추가 할 수 있습니다 .

    override func viewWillAppear(_ animated: Bool) {
        _ = self.tabBarController?.selectedIndex = 1
    }

로드시 자동으로 탭을 목록의 두 번째 항목으로 이동하지만 사용자가 언제든지 해당보기로 수동으로 돌아갈 수 있습니다.


답변

viewController는 UITabBarControllerDelegate 의 자식이어야합니다 . 따라서 SWIFT 3 에 다음 코드를 추가하기 만하면됩니다.

self.tabBarController?.selectedIndex = 1

답변

@codester의 답변을 확장하려면 확인한 다음 할당 할 필요가 없으며 한 단계로 수행 할 수 있습니다.

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    if let tabBarController = self.window!.rootViewController as? UITabBarController {
        tabBarController.selectedIndex = 1
    }

    return true
}

답변

일반적인 애플리케이션에는 UITabBarController가 있으며 3 개 이상의 UIViewController를 탭으로 포함합니다. 이 경우 UITabBarController를 YourTabBarController로 서브 클래 싱 한 경우 다음과 같이 간단히 선택한 인덱스를 설정할 수 있습니다.

selectedIndex = 1 // Displays 2nd tab. The index starts from 0.

다른보기에서 YourTabBarController로 이동하는 경우 해당보기 컨트롤러의 prepare (for segue 🙂 메서드에서 다음을 수행 할 수 있습니다.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        if segue.identifier == "SegueToYourTabBarController" {
            if let destVC = segue.destination as? YourTabBarController {
                destVC.selectedIndex = 0
            }
        }

Xcode 10 및 Swift 4.2에서 탭 설정 방법을 사용하고 있습니다.


답변

스위프트 5

//MARK:- if you are in UITabBarController 
self.selectedIndex = 1

또는

tabBarController?.selectedIndex = 1