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.
Related
I enabled ARC and using ios 7 for my app.With out using xib i am doing programming.But i am unable to navigate from one view controller to another view controller.Created obj in .h file for a class.
In .h file
#property(nonatomic,strong)CountriesViewController *countryViewController;
In .m file in a button action.
countryViewController = [[CountriesViewController alloc] init];
[self.navigationController pushViewController:countryViewController animated:YES];
You need to add navigation Controller in AppDelegate like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In your ViewController, navigate like this,
self.countryViewController = [[CountriesViewController
alloc]initWithNibName:#"CountriesViewController" bundle:nil];
[self.navigationController pushViewController:self.countryViewController animated:YES];
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;
i need to set action for my UITabbar, As when i click my tabbaritem it should go to an action which i am having in another viewcontroller.
but it is redirecting to UITableview
here is my code -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// create our table view controller that will display our store list
FirstViewController *firstViewController = [[EzMasterViewController alloc] init];
// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[navController.tabBarItem setTitle:#"Home"];
[navController.tabBarItem setImage:[UIImage imageNamed:#"53-house.png"]];
SecondViewController *secondViewController = [[SecondViewController alloc] init];
// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[navController1.tabBarItem setTitle:#"Scan"];
[navController1.tabBarItem setImage:[UIImage imageNamed:#"195-barcode.png"]];
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
// create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
[navController2.tabBarItem setTitle:#"Map"];
[navController2.tabBarItem setImage:[UIImage imageNamed:#"07-map-marker.png"]];
// add our view controllers to an array, which will retain them
NSArray *viewControllers = [NSArray arrayWithObjects:navController, navController1,navController2, nil];
// add our array of controllers to the tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];
// set the tab bar controller as our root view controller
[self.window setRootViewController:tabBarController];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
By clicking on SecondViewController tabbaritem it should redirect to the action,
where the action is in FirstViewController.
You should consider implementing the UITabBarControllerDelegate's two methods:
– tabBarController:shouldSelectViewController:
– tabBarController:didSelectViewController:
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.)
I am working on a small app, according to the requirement the app should have a tabBarItem with 3 items. For this I have programmatically created the tabBarController in the AppDelegate.m file and added the 3 different viewControllers, instantiated them and everything is working good. I see the tabBarItems and all views are working. In one of the views lets say in SecondViewController I show a popOverController where I used a UITableView and populate it with items. When I click one of the items it should show another view lets say sendFeedback. Until there everything is working fine, but as soon as this sendFeedback is presented as the modal view, it occupies the whole app i.e it hides the tabBarItem.
I present the important pieces of code here for review:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
viewController1.title = #"First";
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController2.title = #"Second";
UITableViewController *tableView3 = [[tableViewController alloc]initWithNibName:#"tableViewController" bundle:nil];
tableView3.title = #"Third";
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, tableView3 ,nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
[viewController1 release];
[viewController2 release];
[tableView3 release];
return YES;
}
In my popOverViewController.m file I am checking which row is selected in the table according to that I present the view
#pragma mark - TableView Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
sendFeedback *sendEmailViewController = [[sendFeedback alloc]initWithNibName:#"sendFeedback" bundle:nil];
downLoad *downloadFilelViewController = [[downLoad alloc]initWithNibName:#"downLoad" bundle:nil];
if (indexPath.row == 0)
[self presentModalViewController:sendEmailViewController animated:YES];
else
[self presentModalViewController:downloadFilelViewController animated:YES];
}
Can anyone guide me how to overcome this with the multiple views. In case if anyone requires more information from my side I would be glad to provide.
NOTE: It is the same with the other view (downLoad) as well
EDIT: Here is how I am initializing my PopOverController in the AppDelegate.m file
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([viewController isKindOfClass:[SecondViewController class]]){
NSInteger index = [[self tabBarController] selectedIndex];
CGRect buttonFrame = [[[[[self tabBarController] tabBar] subviews] objectAtIndex:index+1] frame];
PopOverViewController *popoverView = [PopOverViewController new];
popoverView.contentSizeForViewInPopover = CGSizeMake(250, 85);
popover = [[UIPopoverController alloc]initWithContentViewController:popoverView];
NSLog(#"X:%f Y:%f",buttonFrame.origin.x,buttonFrame.origin.y);
[popover presentPopoverFromRect:buttonFrame inView:self.tabBarController.tabBar permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
Thanks
Modal view controllers are used to "block" your application and fulfill a task before you can proceed. So modal view controllers are not what you want to use.
Instead wrap your controllers which have to be shown in the popover in a navigation controller. In the tableView:didSelectRowAtIndexPath: method you can push the corresponding view controller to the navigation stack.
To slove your problem:
At the place where you create the popovercontroller initialize it with a new UINavigationController. And the navigation controller you have to initialize with a rootviewcontroller namely PopOverViewController.m.
PopOverController *popoverContentController = [[PopOverController alloc] init];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:popoverContentController];
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContentController];
And in PopOverController.m
if (indexPath.row == 0)
[self.navigationController pushViewController:sendEmailViewController animated:YES];
else
[self.navigationController pushViewController:downloadFilelViewController animated:YES];