2016-05-05 13 views
2

Ich versuche herauszufinden, wie ich die Ansichten von einem Ansichtscontroller zu einem ersten Ansicht-Controller in einem Tab-Leiste-Controller programmgesteuert wechseln kann, wenn ich eine Taste drücke.Programmatically wechseln Ansicht Controller in Ansichten anzeigen Tab-Leiste Controller

Momentan habe ich einen View-Controller mit drei Tasten. Wenn ich einen Knopf drücke, möchte ich dann wechseln. Dies ist der folgende Code, den ich für diesen Bildschirm habe. Es heißt der zweite View-Controller.

import UIKit 

    class SecondViewController: UIViewController { 

    //Button Outlets 
    @IBOutlet var ButtonEndOfShift: UIButton! 
    @IBOutlet var ButtonMultiTimes: UIButton! 
    @IBOutlet var ButtonEndAndLunch: UIButton! 

    override func viewDidLoad() { 

     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     //hides tab bar controller 
     self.tabBarController?.tabBar.hidden = true 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //Changes screen for End Of Shift Button 
    @IBAction func ChangeNextViewLow(sender: AnyObject) { 


     self.performSegueWithIdentifier("segue", sender: nil) 
    } 

    //Changes screen for Lunch and End Of Shift Button 
    @IBAction func ChangeNextViewMedium(sender: UIButton) { 
    } 

    //Changes screen for Multiple Times button 
    @IBAction func ChangeNextViewHigh(sender: UIButton) { 
    } 

} 

enter image description here

+0

Satz TabBarController View-Controller auf Hahn von Taste zu verankern –

+0

Warum Sie segue Durchführung sind? Möchten Sie nicht von einem Tab zu einem anderen Tab des Tab-Bar-Controllers wechseln? – Adeel

Antwort

2

Ich habe UITabBarController in Storyboard wie unten hinzugefügt bitte Bilder sehen.

enter image description here enter image description here

Dann habe ich für Ihre Hilfe folgende functions geschrieben.

// For navigate to Tabbar Controller 
    @IBAction func btnClick() { 
     self.performSegueWithIdentifier("GoToTabBar", sender: nil) 
    } 

    // For switching between tabs 
    func switchTab (index : Int) { 
     self.tabbarController.selectedIndex = index 
    } 

können Sie legen auch UITabBarController wie Ihre Anwendung RootViewController.

+0

self.performSegueWithIdentifier hat funktioniert. Vielen Dank –

+0

Willkommen :-) @AlexPriest –

1

Implementieren Code für didFinishLaunchingWithOptions

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
firstViewController *firstTab = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil]; 
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:firstTab]; 

secViewController *secTab = [[firstViewController alloc] initWithNibName:@"secViewController" bundle:nil]; 
UINavigationController *navCntrl2 = [[UINavigationController alloc] initWithRootViewController:secTab]; 

thirdViewController *thirdTab = [[thirdViewController alloc] initWithNibName:@"thirdViewController" bundle:nil]; 
UINavigationController *navCntrl3 = [[UINavigationController alloc] initWithRootViewController:thirdTab]; 

UITabBarController *tabBarController = [[UITabBarController alloc] init]; 
tabBarController.viewControllers = @[navCntrl1, navCntrl2, navCntrl3]; 

[self.window setRootViewController:tabBarController]; 
[self.window makeKeyAndVisible]; 
Verwandte Themen