hai ,
i have coded in UITableview in the method as follows.but when i touch the cell or row ,it wont
go to the next page(navigation did not work).have i to declare navigation conroller in other file.but i have coded app delegate in applicationdidfinishmethod for tab bar through dynamic.how can i link navigation?
the code:
UITableview;(TableViewController)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubController *nextController = [[SubController alloc] init];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
appdelegation:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
tabBarController.navigationItem.title = #" News";
TableViewController *rtbfViewController = [[TableViewController alloc]
init];
rtbfViewController.tabBarItem.title = #"News";
InfoViewController *infoViewController = [[InfoViewController alloc]
initWithStyle:UITableViewStyleGrouped];
infoViewController.tabBarItem.title = #"Info";
tabBarController.viewControllers = [NSArray
arrayWithObjects:rtbfViewController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
The problem is, that you don't have a UINavigationController, so self.navigationController in your TableViewController is nil (and thus messages sent to this property are ignored). You should modify your code in the app delegate as follows:
// [...] create tab bar view controller...
// create navigation controller with TableViewController instance as root view controller
TableViewController *rtbfViewController = [[TableViewController alloc] init];
rtbfViewController.tabBarItem.title = #"News";
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rtbfViewController];
// [...] create other view controllers
// NOTE: add the navigation controller to the tab bar controller, rather than the TableViewController
tabBarController.viewControllers = [NSArray arrayWithObjects:navController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
And don't forget to release your view controllers afterwards:
[rtbfViewController release];
[navController release];
[infoViewController release];
Related
i implemented a split view in my app. When my app launch it shows fine.
masterview=Leftside view & detailView = Home view
But my master view also contains 2 Table view. when i clicked the any row of table view, the detail view(Now, class view) is not get display.
Mean i want to disply display mutiple detail view as per master view's table row selected.
my code for split view is as follow:
// AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LeftViewController *masterViewController = [[LeftViewController alloc] initWithNibName:#"LeftViewController_iPad" bundle:nil] ;
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController] ;
HomeViewController *detailViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
masterViewController.home_Detail = detailViewController;
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];
}
//LeftView.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.appDelegate.splitViewController viewWillDisappear:YES];
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeAllObjects];
if (tableView==tbl_class)
{
self.class_VC=[[Class_Vice_ViewController alloc]initWithNibName:#"Class_Vice_ViewController" bundle:nil];
[[NSUserDefaults standardUserDefaults]setInteger:[[[classNames objectAtIndex:indexPath.row]valueForKey:#"class_id"]intValue] forKey:#"psel_class"];
NSLog(#"%d",[[[classNames objectAtIndex:indexPath.row]valueForKey:#"class_id"]intValue]);
[viewControllerArray addObject:self.class_VC];
self.appDelegate.splitViewController.delegate = self.class_VC;
[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
[class_VC viewWillAppear:YES];
}
}
Help me to Solve this issue
The offending line is this one in the didSelectRowAtIndexPath: method:
[self.appDelegate.splitViewController viewWillDisappear:YES];
I don't know why you put that in, but if you remove it, it should work. You also don't need this line:
[self.class_VC viewWillAppear:YES];
While it is quite common to override viewWillAppear: and viewWillDisappear:, you almost never call them like you're doing.
I have three tab bar application and my tabs is TAB1, TAB2 , TAB3 i have write the following code in TAB1 view controller to detect which tab user have pressed
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"tab selected: %#", item.title);
}
But this delegate never get called
i have setup my tab in appdelegate.m something like that
- (void)setupTabBar
{
self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:#"MyWorkListViewController"
bundle:nil];
self.askHRViewController = [[AskHRViewController alloc] initWithNibName:#"AskHRViewController"
bundle:nil];
self.moreViewController = [[MoreViewController alloc] initWithNibName:#"MoreViewController"
bundle:nil];
self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:#"BookLeaveViewController"
bundle:nil];
self.helpViewController = [[HelpViewController alloc] initWithNibName:#"HelpViewController"
bundle:nil];
// Create navigation controllers
workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];
askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];
bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];
moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];
helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];
[self setTabBarImagesAndText];
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
}
you can detect selected Tabbar Item like this way:-
as par your code you just need to add this line
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
in .h file define like
#interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
//declare your Tabbar controller with #proparty
}
in .m file
//#synthesize here your Tabbar controller
- (void)viewDidLoad
{
self.yourTabbarControler.delegate=self;
[super viewDidLoad];
}
and now put this delegate of UITabbarController
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(#"controller class: %#", NSStringFromClass([viewController class]));
NSLog(#"controller title: %#", viewController.title);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}
You have to "wire up" the delegate, either using code (in ViewDidLoad etc.) or in interface builder.
Look at this answer which explains how to connect a textView delegate (it's pretty much the same): https://stackoverflow.com/a/1785366/764575
I am developing one application which includes view controller because my first two pages content only view not tab-bar.after that i have created run time tab-bar controller using this code
UIViewController *viewcontroller1 = [[viewcontroller1 alloc] initWithNibName:#"viewcontroller1" bundle:nil];
viewcontroller1.title = #"sometext";
viewcontroller1.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
UIViewController *viewcontroller2 = [[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil];
viewcontroller2.title = #"sometext";
viewcontroller2.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
tbc = [[UITabBarController alloc] initWithNibName:#"viewcontroller1" bundle:nil];
tbc.viewControllers = [NSArray arrayWithObjects: viewcontroller1,viewcontroller2, nil];
tbc.selectedViewController = viewcontroller1;
//// NSLog(#"Selected index = %d of %d", tbc.selectedIndex, [tbc.viewControllers count]);
[self presentModalViewController:tbc animated:NO];
it is working properly but in my second view i want tab-bar controller and navigation-controller both.
so in viewcontroller2 i have implemented code like that it's giving me navigation controller but it's hiding tab-bar controller
- (void)viewDidLoad
{
nvc = [[UINavigationController alloc] initWithRootViewController:[[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil]];
[self presentModalViewController:nvc animated:NO];
[nvc release];
[super viewDidLoad];
}
so please help me what to i do so i can get both tab-bar controller & navigation-controller in this viewcontroller2.??
please guide me.
You can create new class for tabbar and in UI Design time you can add navigation controller as tab and set view in navigation controller. Whenever you want add tabbar controller in that page create object of tabbarcontroller class object and add it on view.
My program is not supporting the UIInterfaceOrientation. Program will not support the UIInterfaceOrientation after I add the UITabBarItem.Please give a solution. Also I added the navigationController.
Here is my code.
-(void) applicationDidFinishLaunching:(UIApplication *)application {
//I create my navigation Controller
//UINavigationController *navigationController;
//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];
// Icreate the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
// I create the view controller attached to the first item in the TabBar
sivajitvViewController *firstViewController;
firstViewController = [[sivajitvViewController alloc]init];
navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
firstViewController.navigationItem.title=#"Gallery";
//viewController.tabBarItem.image = [UIImage imageNamed:#"natural.jpg"];
navigationController.tabBarItem.image = [UIImage imageNamed:#"Gallery.png"];
navigationController.tabBarItem.title = #"Gallery";
//navigationController.headerTitle = #"Some Title";
[localControllersArray addObject:navigationController];
[navigationController release];
[firstViewController release];
// I create the view controller attached to the second item in the TabBar
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
navigationController.tabBarItem.image = [UIImage imageNamed:#"News.png"];
navigationController.tabBarItem.title = #"News";
[localControllersArray addObject:navigationController];
[navigationController release];
[secondViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
If you have UITabBarController, all the tabs should support your interface orientation. So if you have 3 tabs and 2 of them support portrait and landscape, but the last one supports only portrait, you application will never turn to landscape.
I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:
UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];
tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];
Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.
Thanks
Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):
#interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...
- (IBAction) onButtonPressed:(id)sender
{
UITabBarController* tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
[[self tabBarController] setSelectedIndex:1];
tabBarController.delegate = self;
[self presentModalViewController:tabBarController animated:NO];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if (viewController == viewController3)
{
[self dismissModalViewControllerAnimated:NO];
return NO;
}
return YES;
}
I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.
if I understand right, you can just remove your tabbar's view from superview. smth like
[[tabBarController view] removeFromSuperview];
if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.
Is this what you're trying to do?
This automatically created with a UINavigationController upon pushing to a child view controller.
[self.navigationController pushViewController:yourChildViewController animated:YES];