Unable to set action for UITabbarItem Programmatically - objective-c

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:

Related

UITabBarController doesn't carry through when i push a new view from a UINavigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[FirstViewController alloc] init];
//Create UITabBarController
UITabBarController *theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSSArry arrayWithObjects: fvc, svc, nil];
[theTabBarController setViewControllers:viewControllers];
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController
alloc]initWithRootViewController:theTabBarController];
[[self window] setRootViewController:theNavigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Then in the First View Controller i do a push to a second view
- (IBAction)Page2:(id)sender {
SBHomePageDetailViewController *detailPageViewController = [[SBHomePageDetailViewController alloc] init];
// Pushing to the stack
[[self navigationController] pushViewController:detailPageViewController animated:YES];
}
Now my UI shows the second view, however, the UITabBarController is missing. When i navigate back the tab bar view is back. How do I keep the tab bar controller visible in all ui screens?
Into AppDelegate.h file make property of theTabBarController:
#property (nonatomic, strong) UITabBarController *theTabBarController;
And here how I changed your didFinishLaunchingWithOptions method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[SecondtViewController alloc] init];
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController
alloc]initWithRootViewController:fvc];
//Create UITabBarController
self.theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects: theNavigationController, svc, nil];
[self.theTabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:theNavigationController];
[[self window] addSubview:self.theTabBarController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The problem in the code is that it tries to initialize a UITabBarController as the rootViewController of a UINavigationController in this line:
// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController alloc]initWithRootViewController:theTabBarController];
From the docs:
rootViewController:
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
Try removing that line and, per #rmaddy's suggestion, put each View Controller in a Navigation Controller. Then set those Navigation Controllers as the Tab Bar Controller's VCs and set the App's RootViewController to the Tab Bar Controller:
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[SecondtViewController alloc] init];
// Create the first UINavigationController
UINavigationController *firstNavigationController = [[UINavigationController
alloc]initWithRootViewController:fvc];
// Create the second UINavigationController
UINavigationController *secondNavigationController = [[UINavigationController
alloc]initWithRootViewController:svc];
//Create UITabBarController
theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects: firstNavigationController, secondNavigationController, nil];
[theTabBarController setViewControllers:viewControllers];
[[self window] setRootViewController: theTabBarController];

Set title and add button to navigation controller

I created a navigation controller but i can not set title or add button to the navigation bar.How to do that?
This is the code of application DidFinishLauchingOption in file AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.view = [[ViewController alloc] init];
self.window.rootViewController = self.view;
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Thanks in advance.
You need to set the rootViewController property of your window object to the navigation controller, rather than your instance of `ViewController. This should point you in the right direction:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create and configure a window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// Create a view controller
UIViewController *viewController = [[ViewController alloc] init];
// Create a navigation controller and set its root view controller to the instance of `ViewController`
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// Add the navigation controller to the window
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
// ...
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the view controller's title
self.title = NSLocalizedString(#"View Controller", #"");
// Add a navigation bar button
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonPressed:)];
}
- (void)refreshButtonPressed:(id)sender
{
// Do something when the refresh button is pressed.
}
// ...
#end
To create your initial setup, you create a navigation controller with your view controller and set it as the root view controller of your app delegate window:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//create window
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
//create and set root view controller
[[self window] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]]];
//make window key and visible
[self.window makeKeyAndVisible];
//bail
return YES;
}
Then in your view controller, set the title and add your navigation item:
- (void)viewWillAppear:(BOOL)animated
{
//call parent implementation
[super viewWillAppear:animated];
//set view controller title
[self setTitle:#"Root View Controller"];
//add navigation bar button
[[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Button Title" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBarButtonItemEvents:)]];
}
And listen for button events via:
- (void)handleBarButtonItemEvents:(id)sender
{
//
}

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.)

UITabBarController with a UIPopOverController with Multiple Views

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];

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.