问题
I need to add an tab bar in my view with out tab bar controller, because in my prepareForSegue
function I pass data to my view whitch contains the tab bar.
I add the tab bar from the object library with its items, and in my view class I add UITabBarDelegate. my code is like this:
class SchedaCompletaVC: UIViewController, UITabBarDelegate {
var event:Event! // data passing from prepareforsegue
@IBOutlet var mainTabBar:UITabBar!
var descrizioneTab:UIViewController! //if i click on first tab, so my default view
var mappaTab:UIViewController! // second tab
override func viewDidLoad() {
super.viewDidLoad()
println("event \(event.eventId)")
// Do any additional setup after loading the view.
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
switch (item.tag) {
case 1:
self.view.insertSubview(descrizioneTab.view.belowSubview(mainTabBar)) //here I had error
break;
case 2:
//if second tab is selected
break;
default:
break;
}
}
I got this code from an tutorial whitch is for objective-c, I tried to convert it in swift.
My code doesn't work, I don't know what to do?
Please help. Thank you!
Update: my storyboard is like this:

回答1:
Here is the swift version :-
import UIKit
class ViewController: UIViewController, UITabBarDelegate {
@IBOutlet weak var mainTabBar: UITabBar!
var viewController1: UIViewController?
var viewController2: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
switch item.tag {
case 1:
if viewController1 == nil {
var storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController1 = storyboard.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1
}
self.view.insertSubview(viewController1!.view!, belowSubview: self.mainTabBar)
break
case 2:
if viewController2 == nil {
var storyboard = UIStoryboard(name: "Main", bundle: nil)
viewController2 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
}
self.view.insertSubview(viewController2!.view!, belowSubview: self.mainTabBar)
break
default:
break
}
}
}
Then you should click the UITabBarItem & ctrl drag it to viewcontroller and click delegate.
Tag for Tab Bar Item( for first tab item tag is 1, for second tab item, tag is 2)
StoryBoard identifier for ViewController(ViewController1 and ViewController2, Respectively)
回答2:
I have an Objective-C solution. It works fine, you may convert it into Swift.
ViewController.h file code
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITabBarDelegate>
@property (weak, nonatomic) IBOutlet UITabBar *mainTabBar;
@end
ViewController.m file
#import "ViewController.h"
#import "MyFirstTabViewController.h"
#import "MySecondTabViewController.h"
@interface ViewController ()
{
UIViewController *viewController1;
UIViewController *viewController2;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mainTabBar.delegate=self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
if (viewController1 == nil) {
viewController1 =
[[UIStoryboard storyboardWithName:@"Main"
bundle:NULL] instantiateViewControllerWithIdentifier:@"First"];
}
[self.view insertSubview:viewController1.view belowSubview:self.mainTabBar];
break;
case 2:
if (viewController2 == nil) {
viewController2 =
[[UIStoryboard storyboardWithName:@"Main"
bundle:NULL] instantiateViewControllerWithIdentifier:@"Second"];
}
[self.view insertSubview:viewController2.view belowSubview:self.mainTabBar];
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Do not Forget.
1) Tag for Tab Bar Item( for first tab item tag is 1, for second tab item, tag is 2)
2) StoryBoard identifier for ViewController(First and Second, Respectively)

来源:https://stackoverflow.com/questions/27267123/add-tab-bar-without-tab-bar-controller-in-swift