Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.
What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).
Any help would be greatly appreciated.
Dave
[myTabBarController setSelectedIndex:index]
EDIT: Answering the part 2 question from the comment:
You can define a method in AppDelegate for switching to a different tab.
And you can get hold of appdelegate from anywhere and send a message..
something like:
MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate SwitchToTab:index]
alternatively...
[self.parentViewController.tabBarController setSelectedIndex:3];
I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.
Anyhow, I hope this helps someone:
I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:
AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];
When I should have been doing this:
AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];
Newbie mistake.
For this, You just need to take UITabBar controller -
.h File -
UITabBarController *_pTabBarController;
#property (nonatomic, retain) IBOutlet UITabBarController *_pTabBarController;
.m File -
// synthesize it
#synthesize _pTabBarController;
At initial load
// You can write one function to add tabBar -
// As you have already mentioned you have created an array , if not
_pTabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
UINavigationController *theNavigationController;
_pController = [[Controller alloc] initStart];
_pController.tabBarItem.tag = 1;
_pController.title = #"Baranches";
theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
theNavigationController.tabBarItem.tag = 1;
theNavigationController.tabBarItem.image = [UIImage imageNamed:#"icon_branches.png"];
[localViewControllersArray addObject:theNavigationController];
[theNavigationController release];
than you can set index as per your needs
self._pTabBarController.selectedIndex = 0; // as per your requirements
[self.parentViewController.tabBarController setSelectedIndex:3];
Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.
Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:
UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];
Thank you for this post/answers it helped out a lot in my project.
I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.
Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:
FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;
You might have similar code already inside your didSelectedItem..
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item == self.firstTabBarItem)
// Right here
}
else if ...
}
Related
I am trying to implement ECSlidingViewController & I am using segues in my storyboards. I am having a problem where the MainViewController's viewDidLoad is being called twice and
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
is always getting set to null. More information.
LogIn Screen ( InitViewController ) ------through segue---------> MainViewController.
In my MainViewController I have the following code.
-(void)addMenuViewControllerWithGesture{
self.view.layer.shadowOpacity = 0.075f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[rsMenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
}
In My InitViewController (LogIn)
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
MainViewController *controller = segue.destinationViewController;
// setting some values before going to the next view.
controller.id = 0;
controller.name = #"harish";
self.topViewController = controller;
}
By doing this, the MainViewController's viewDidLoad is getting called twice and the below value is always set to null.
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
Hope it's clear. I am kind of stuck here and looking for a solution.
Thanks
Okay, I figured it out. After a long, arduous experience, I figured it out. I asked a similar question here: iOS Adding Menu Causes Crash When Not First View and eventually answered it. I just recently came upon your question.
So let's say you have numerous files (.m and .h for each):
InitViewController
MenuViewController
MainViewController
SpeakersView
The objective was to switch from MainViewController using a button not on the slide menu to SpeakersView, but doing it directly caused the slide menu to be null, as was pointed out by Phillip Mills.
Instead, I put this in InitViewController.h, right underneath #Interface:
#property (nonatomic, retain) NSString *location;
Then this under #implementation in InitViewController.m:
#synthesize location;
I originally had this under ViewDidLoad in InitViewController.m:
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
I changed it to this:
if([location length] == 0){location = #"Main";}
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:location];
That way if the variable was empty, it defaulted to #"Main".
Then in MainView.m, where I execute the code after pressing the button, I have this:
InitViewController *ini;
ini = [self.storyboard instantiateViewControllerWithIdentifier:#"Init"];
ini.location = #"Speakers";
And voilĂ , the view switches just fine to Speakers. More accurately, it switches to InitViewController which automatically switches to Speakers or whatever I set location to.
Hopefully this helps you.
Any questions, let me know.
When a user clicks on a button, I create a UITabBarController with two viewcontrollers attached to it.
I then push the TabBarController onto my navigationController stack and it displays without problems. The problem is trying to set a badge on one of the tab bar items when the tabbarControlelr is loaded, I tried:
[[self.tabBarController.viewControllers objectAtIndex:1] setBadgeValue:#"1"];
and a whole lot of variations of this one, but none gives me a round red button thingy on the tabbar item.
Any suggestions?
Thanks,
Ron
EDIT
Code how I present the tabBarController
Airline_RosterAppDelegate *appDelegate = (Airline_RosterAppDelegate *)[[UIApplication sharedApplication] delegate];
CrewHere *vc = [[CrewHere alloc] initWithNibName:#"CrewHere" bundle:nil];
vc.title = #"Crewlist";
MessagesDetailed *mvc = [[MessagesDetailed alloc] initWithNibName:#"MessagesDetailed" bundle:nil];
mvc.title = #"Messageboard";
[tabbar setViewControllers:[NSArray arrayWithObjects:vc, mvc, nil]];
[tabbar setToolbarItems:[NSArray arrayWithObjects:#"Crewlist", #"Messageboard", nil]];
[appDelegate.navigationController pushViewController:tabbar animated:YES];
The property 'badgeValue' is defined on a UITabBarItem. You would set it with:
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex: 1];
item.badgeValue = #"1";
...
The above won't work, nor will your code, when 'the UITabBarController is loaded.' You can only do it after the controller appears because only at that point can you be sure that all the view elements actually exist. Put it in viewDidAppear:animated: for the tabBarController
[Edit, after your Edit]
You can't call setToolbarItems: with an NSArray of NSString; the array needs UITabBarItem. Instantiate with - (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
Also, it appears that you are mixing tool bar and tab bar - they are different.
I have page-based app. On each page I have 3 uibuttons at the top, uiscrollview with alphabet (uibuttons to sort data in uitable) at the right and uitableview at the center. How to show cell's detail view? If it is necessary to add uinavigationcontroller I can't do this. If I adds it, it disables interaction with my table, buttons and scrollview.
And another question is how to show new data in tableview and scrollview when goes to next page??
I have rootViewController class and DataViewController class.
rootViewController listing:
#interface RootViewController ()
#property (readonly, strong, nonatomic) ModelController *modelController;
#end
#implementation RootViewController
#synthesize pageViewController = _pageViewController;
#synthesize modelController = _modelController;
#synthesize navContr = _navContr;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
//[self presentModalViewController:navContr animated:YES];
self.pageViewController = [[[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil] autorelease];
self.pageViewController.delegate = self;
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = [NSArray arrayWithObject:startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
self.navContr = [[UINavigationController alloc] initWithRootViewController:self.pageViewController];
[self.view addSubview:self.navContr.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers){
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]){
[recognizer setEnabled:NO];
}
}
}
After few manipulations it works but I need help to get it work fine!
So now it looks like this
Next question: how do I remove the brown space at the top???
::UPDATE::
Problem solved. It needs just to set y-axis position of UINavigationController to -20 ;)
i'm not sure if this link about creating navigation-based project may help you.. (http://iosmadesimple.blogspot.com/2012/08/navigation-based-project-doing-it.html)
From that tutorial, there's a class called SampleViewController, subclass of UIViewController. You might want to put a tableView in SampleViewController.xib file. Then in your SampleViewController.h file, add an IBOutlet UITableView* yourTable property and synthesize it. Connect it to your tableView in your .xib file. //Or you may do it programmatically
in your SampleViewController.h, make your interface header look like this.. I think you already know this...
#interface SampleViewController:UIViewController < UITableviewDelegate, UITableViewDatasource >
in your SampleViewcontroller.m, under viewDidLoad method, set the table delegate and datasource to self:
yourTableView.delegate = self;
yourTableView.datasource = self;
Afterwhich, you implement tableView delegate and datasource methods... //you already know those since you were already able to show a tableview ;)
one of these methods is the "tableview:didSelectAtIndexpath:" --> this is the part wherein you can put your code when you click one of the cells.
Let's assume you have the DetailsViewController Class, this is the class you would like to show after clicking a cell and show its details.
DetailsViewController Class must have a variable that will accept the data you would like to show. Let's say, an NSString *detailsMessage; //do the #property and #synthesize thing...
Let's go back to SampleViewController.m file, under tableview:didSelectAtIndexpath: Method:
inside that Method.. put these codes.
DetailsViewController *detailsVC = [[DetailsViewController alloc] init];
detailsVC.detailsMessage = #"The Data you want to pass.";
[self.navigationController pushViewController:detailsVC animated:YES];
I hope this helps. :(
There may be other ways but by far the easiest way is to use a navigation controller. In fact, it is built to do exactly this.
If you don't want the navigationBar then you can hide it in the viewWillAppear function.
[self.navigationController setNavigationBarHidden:YES animated:YES];
Then you can add an other UIViewController to push when the user selects a cell.
Having read your OP again I'm not sure how you are adding your navigationController.
To use a navigationController you create it and load it at start time. You then create your current viewController (the one with the buttons and table etc...) and set this as the rootViewController of the navigationController.
Then you display the navigationController.
Could you explain how you are adding your navigationController as it may help understand what is going wrong.
Thanks
::EDIT::
OK, my assumption was correct.
The way you are using the navigation controller is not how it was intended.
OK, so at the moment your AppDelegate file will have a method Application didFinishLaunching...
It will look something 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 = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You should change it so that it is like this...
first add a property to your appDelegate...
#property (nonatomic, strong) UINavigationController *navigationController;
Then change the didFinishLaunchingMethod to 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 = [[OJFViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This will still show you MainViewController but it will now be contained within a navigationController.
Next in your MainViewController function viewWillAppearAnimated add the line...
[self.navigationController setNavigationBarHidden:YES animated:animated];
This will then hide the navigationBar at the top of the view so you still have access to your buttons.
The you need a new ViewController and xib file (for example DetailViewController).
When the user selects a table row you need to then do something like...
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
//pass in details of which row was selected.
[self.navigationController pushViewController:detailView animated:YES];
This will then display your new view and new viewController. You will also need to write a way of passing the data in (set up a property on the DetailViewController).
Hope this helps.
Obviously, I'm new to iOS development, but I could really use somebody's help. I am building a tab bar application in which I am attempting to load .plists into drill-down table views. The problem is that I can't seem to get this method right, as I am trying to use the navigation controller for a tab in its ViewController. I'm positive that my error is in the second line.
A37dgAppDelegate *AppDelegate = (A37dgAppDelegate *)[[UIApplication sharedApplication] delegate];
AppDelegate.indNavControl *indNavControl;
Subsequently, I get some errors. Here is the code, and I'll point out where the errors are:
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.indNavControl pushViewController:dvController animated:YES];//Property "indNavControl" not found on object of type "IndustriesViewController"
[dvController release];
}
else {
//Prepare to tableview.
IndustriesViewController *indViewControl = [[IndustriesViewController alloc] initWithNibName:#"IndustryView" bundle:[NSBundle mainBundle]];
//Increment the Current View
indViewControl.CurrentLevel += 1;
//Set the title;
indViewControl.CurrentTitle = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
[self.indNavControl pushViewController:indViewControl animated:YES]; //Property "indNavControl" not found on object of type "IndustriesViewController"
indViewControl.tableDataSource = Children;
[indViewControl release];
}
}
Just to be clear, I have imported the header file of my App Delegate. Any help would be greatly appreciated :)
The line AppDelegate.indNavControl *indNavControl; is wrong. I think you don't need this line at all. And while pushing new view controller you can directly use AppDelegate.indNavControl instead of self.indNavControl,
[AppDelegate.indNavControl pushViewController:...
Your theory that the problem is in the second line is correct.
AppDelegate.indNavControl *indNavControl;
that line isn't really doing anything. You need a class where AppDelegate.indNavControl is.
You have a couple of options:
1) if you want indViewControl to be a 'public' property
* in your IndustriesViewController.h, move the declaration of indViewControl there.
#interface
{
WhateverClass *indNavControl;
}
#property (retain) WhateverClass*indNavControl;
#end
2) if you want a private property then add an empty category at the top of your .m with the ivar and property declaration from above.
What the compiler is complaining about is trying to access a property that doesn't exist.
calling self.whateverIvar requires an #property definition.
first create the instance of the appdelegate like and then you can access, whatever variables you have declared in appdelegate as property,synthesize.There is no need to create seperate variable of indNavControl.
I normally use UIViews to make my apps - but this one I am using a navigationcontroller. I am pushing a view to the top where I want to add items to an array. However, I cannot access the main navigation controller methods etc. Here's the set up
1) AppDelegate adds navigation controller
[window addSubview:navigationController.view];
2) In RootViewController I push a new UIView
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self presentModalViewController: addNewViewController animated:YES];
3) From the AddNewViewController I then want to access the main RootViewController - but cannot seem to access anything. All the methods etc are declared as I've done it before (using UIViews). This code cannot find anything in the RootViewController.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController myFunction];
I have myFunction in the RootViewController.h file. I have used this method before, but never with a navigation controller. I guess it's something to do with hte stack - but I cannot find out what I've done wrong!
Help is much appreciated!!
* UPDATE *
I have now used
AddNewViewController *addNewViewController = [[AddNewViewController alloc] init];
[self.navigationController pushViewController:addNewViewController animated:YES];
to push the view controller. In my code, I am trying to access the tableview to reload it with the following code
[self.navigationController.mainTableView reloadData];
but the mainTableView is not accessible. I have declared and sync'd it but still cannot see it. I also tried to loop through and use the element[0] of the stack (as below) but didn't get anywhere with that either!
NSArray *controllers = [[NSArray alloc] initWithObjects:self.navigationController.viewControllers, nil];
UIViewController *tmpController = [[UIViewController alloc] init];
tmpController = [controllers objectAtIndex:0];
First of all,
[self presentModalViewController: addNewViewController animated:YES];
does something different than
[self.navigationController pushViewController:addNewViewController animated:YES];
The names of those methods should speak for themselves.
And appDelegate.navigationController is not your rootViewController. It is the UINavigationController. You can get an NSArray of viewControllers attached to the navigationcontroller with self.navigationController.viewControllers. Item at index 0 should be your rootViewController.
If you push the viewController on the navigationControllers stack there is no need to use the appdelegate. You can access the navigationController with self.navigationController.