Add a UITabBarController to a UIViewController (Xcode 4.6) - objective-c

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

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

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.

Related

"initWithNibName" method not being called

NSString *nibName = #"SampleViewController"
UIViewController *controller = [[UIViewController alloc] initWithNibName:nibName bundle:nil];
using the above code to move to the "SampleViewController" xib... its working fine but initWithNibName method not triggering in the SampleViewController.
anyone can help me to trigger lifecycle method in SampleViewController
UIViewController is parent from which SampleViewController is implemented. So instead of UIViewController you need to use SampleViewController class instance.
SampleViewController *controller = [[SampleViewController alloc] initWithNibName:#"SampleViewController" bundle:[NSBundle mainBundle]];

changing default view with master/detail application

can you help me with this one? I'm creating an app and I'm using a master/detail application, then I added a new class called MenuViewController and I want it to be the initial view to load when my app starts. I was able to do it but my MasterViewController does not work any more, everytime i click an item in the table view it's suppose to show another view called DetailViewController but ever since I changed the default view it doesn't show the DetailViewController anymore
Here's a piece of code i've done
in the appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
MenuViewController *menuView = [[[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil]autorelease];
MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = menuView;
[self.window makeKeyAndVisible];
return YES;
}
and in my MenuViewController.m
// Created by Jan Arman Capistrano on 2/6/13.
// Copyright (c) 2013 Jan Arman Capistrano. All rights reserved.
//
#import "MenuViewController.h"
#import "MasterViewController.h"
#interface MenuViewController ()
#end
#implementation MenuViewController
-(IBAction)nextView:(id)sender
{
MasterViewController *masterViewc = [[MasterViewController alloc]initWithNibName:nil bundle:nil];
[self presentViewController:masterViewc animated:YES completion:nil];
}
It seems your MasterViewController is instantiated from a XIB. If so, check that the UITableView has its delegate and datasource bindings (propably the MasterViewController itself)
have you tried to set a breakPoint in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, to see if it's actually called ?

how to create tab bar controller in a view controller not in app delegate? IOS 4.1

I am creating a tab bar application, but i want to create tab bar controller not in Main.xib by using interface builder. Because my app has no Main.Xib. So I either should do it in ViewController.xib or programmatically in a controller/appdelegate. I couldnt find any good tutorial or example for it.
In my app i have
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
ViewController.xib
My application starts with view of ViewController.xib know i want to add not a tab bar but a tab bar controller which will always stay bottom of views. How can i do that?
appdelagete.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I have tried to do it programatically by looking at apple developer docs but couldnt figure it out.
Thanks in advance for any example code
I found this working great
appdelegate.h
#property (nonatomic, retain) UITabBarController *rootController
appdelegate.m
UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.rootController = [[[UITabBarController alloc] init] autorelease];
self.rootController.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
self.window.rootViewController = self.rootController;
This will create a tab bar with 3 views
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view2, nil];
self.window.rootViewController = self.tabBarController;
///AppDelegate.m file : didFinishLaunchingWithOptions method
//initiate window
window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//initiate viewcontrollers
FirstViewController *aFirstViewController = [FirstViewController new];
SecondViewController *aSecondViewController = [SecondViewController new];
aFirstViewController.tabBarItem.title = #"First";
aFirstViewController.tabBarIte
aSecondViewController.tabBarItem.title = #"Second";
gTabBarController = [[UITabBarController alloc]init];
gTabBarController.viewControllers = #[aFirstViewController ,aSecondViewController];
//show the main window and also make it key
[window makeKeyAndVisible];
window.rootViewController = gTabBarController;

Display an Image in DetailViewController Sent by MasterViewController using Apple SplitView Template

i'm working on an application and i'm still a beginner with ios programing.
I'm asking for your help because i used the apple MasterDetailView template.
I'm generating a list of file stored in my application, which i display within my MasterView(TableView).
When i click on one of the files contained in my list i generate an image in relationship with this file and i would like to display it in my DetailView.
Is there a solution to do it without destroying all my application :)
Thanks for you help guys!!
This is my Delegate and how my controllers are declared:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}
Assuming it's your master controller that's responsible for creating the image.... You would normally create a property in your detail controller to hold a reference to the image. Inside the method tableView:didSelectRowAtIndexPath: of the master, you would pass that reference to the detail view controller.
(Alternatively, pass the file information instead and let the detail controller create the image.)

How to add a new tab in xcode 4.2

I started a project in Xcode 4.2 using tab view template. In the app delegate I added a third tab by code just like first and second tabs. Then I created a third view controller class with a nib file.
When I run this app, I see all three tabs but when I click on the third tab, it crashes.
I noticed the first and second nib files have a dark bar at the bottom(probably representing the tab bar) of the view but the new third nib file that I created lacks it. Any idea how I make this third tab work?
Thanks
This is how I add the third view controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController_iPhone" bundle:nil];
} else {
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPad" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In the tab bar controller make sure the class of the view controller for the tab is the same as the view controller class you created.
Also check the logs, it will probably have a very informative message for you as to why it crashed.
Edit:
Never mind, you are passing in an un-initialized view controller for viewController3. Set all of those initial values to nil.