Reputation: 1099
I am studying Objective-C and I started to create an App while I am learning it. I have created a "Single View Application" with two "View Controllers" called "ViewController" and "secondViewController", after that I thought to add an "UITabBarController", so following a tutorial I used the following code in the AppDelegate.m
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];
UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Music" image:[UIImage imageNamed:@"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Here is the ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
If I run the application, the tab bar is working but I have a blank screen and not the ViewController I have created. Just because I am a noob, could you please tell me how to link or show the view controllers respectively in each tab?
Please be so kind to say everything also the obvious things. Thank you in advance
Upvotes: 0
Views: 1920
Reputation: 4914
Since you are beginner I would highly suggest you to use storyboards, that will help to manage the interface much more easier because you will not have to code almost anything.
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
I am just guessing but your problem might be that you directly init
your view controller , you should call initWithNibName:
method in your didFinishLaunchingWithOptions
. The initWithNibName:
is something that you call when you create a controller instance from a nib file.
So basically initWithNibName:
is called when the NIB is loaded and instantiated.
I dont know your ViewContoller class names but you should change your view controllers init methods
in your app delegate.m
#import "ViewController.h"
#import "secondViewController.h"
UIViewController *viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController
UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController
and please use Upper Case Chars and detailed names for class name secondViewController is not good programming practice
Upvotes: 1
Reputation: 1739
The tabbar controller shows the views, you created in your code. That's the expected behaviour, since you are creating completely new views.
If you created "ViewController" and "secondViewController" in Interface Builder, then you'll have to load the nibs - instead of creating new views - and tell the tabbar controller to use these.
Basically you'll have to change your lines:
UIViewController *viewController1 = [[UIViewController alloc] init];
to something like
UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"view1" bundle:nil];
Please see initWithNibName:bundle:
for the details.
Upvotes: 0