Segmented Control to Swap ViewControllers in a Navigation Controller - iphone

Here is the setup of my iPhone App:
I have a UITabBarController that has 4 View Controllers (1 UINavigationController & 3 UIViewControllers).
Onload my app is defaulted to the UINavigationController, where there is a grouped UITableView which gives two navigation options, when the user hits the first option the relevant UITableViewController (Contains a list of locations) is pushed onto the stack.
What I would like to happen on screen is to have a UISegmentedControl that has two options, a "List View" (which the user sees by default when the UIViewController gets pushed) ans a "Map View" that will allow the relevant locations to be represented on the map.
I use the follwing code to create the UISegmentedControl on the NavigationBar:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[[self navigationItem] setTitleView:segmentedControl];
[segmentedControl release];
And this is the method that the UISegmented control will call when changed:
- (void) segmentAction:(id)sender
{
UISegmentedControl *segmentControl = sender;
if([segmentControl selectedSegmentIndex] == 0)
{
NSLog(#"LIST VIEW CHOSEN!");
}
else
{
NSLog(#"MAP VIEW CHOSEN!");
}
}
So basically my question is what is the correct/best way to implement what I am trying to achieve, I dont want to push the controller onto the navigation stack, I just want to toggle it in place (possibly with animation). All interfaces are built in IB.
One way i tried to do it, that worked but didn't feel like it was the correct was by creating two separate UIViews in my nib file (One for the List View and one one for the Map view), and then using setView: appropriately.. but then I thought shouldn't each of these views have there own controller and/or own nib?
- (void) segmentAction:(id)sender
{
UISegmentedControl *segmentControl = sender;
if([segmentControl selectedSegmentIndex] == 0)
{
NSLog(#"LIST VIEW CHOSEN!");
[self setView:listView]; //Declared as an UIView IBOutlet
}
else
{
NSLog(#"MAP VIEW CHOSEN!");
[self setView:mapView]; //Declared as an UIView IBOutlet
}
}
Some semi related examples I have come across use "removeFromSuperview" and "addSubview" and this has somewhat confued me, i'm relatively new to Cocoa Touch development, so any help getting my head around this would be appreciated.
Thanks

There are a couple of ways to handle this, if you are simply going between a TableView (that is already on the UINavigationController stack) and a mapview that goes with this SPECIFIC UITableView, you could have the segmentedControl display the mapviewController modally, that is presentModalViewController:. This does have some drawbacks as to functionality, but it could be what you are looking for if the mapview will be relatively simple.
Another way of implementing this, and probably the most robust way, is to have one view controller that handles both the table view and the map view. This is ideal if they have the same data source for certain.
One other way is to have two view controllers, one each for the table view and map view, and literally do pushes and pops on the navigation controller stack in your segmentAction method.
Ex:
- (void) segmentAction:(id)sender
{
UISegmentedControl *segmentControl = sender;
if([segmentControl selectedSegmentIndex] == 0)
{
NSLog(#"LIST VIEW CHOSEN!");
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:listViewController animated:YES];
}
else
{
NSLog(#"MAP VIEW CHOSEN!");
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:mapViewController animated:YES];
}
}
I would make sure to read the Human Interface Guidelines, as well as the documentation of how view controllers and navigation controllers work.

I came across this while looking for a solution to the same problem. Seems to work pretty well for me.

switch (self.menu.selectedSegmentIndex)
{
case 0 :
// setting text to name label
boynamelabel.text=[boysnamearray objectAtIndex:0];
girlnamelabel.text=[girlsnamearray objectAtIndex:0];
// setting text to percent label
boyspercentlabel.text=[boyspercentarray objectAtIndex:0];
girlspercentlabel.text=[girlsprecentarray objectAtIndex:0];
// setting image to image view
boysimage.image=[UIImage imageNamed:#"boy1.jpeg"];
girlsimage.image=[UIImage imageNamed:#"girl1.jpeg"];
break;
case 1:
// setting text to name label
boynamelabel.text=[boysnamearray objectAtIndex:1];
girlnamelabel.text=[girlsnamearray objectAtIndex:1];
// setting text to percent label
boyspercentlabel.text=[boyspercentarray objectAtIndex:1];
girlspercentlabel.text=[girlsprecentarray objectAtIndex:1];
// setting image to image view
boysimage.image=[UIImage imageNamed:#"boy2.jpeg"];
girlsimage.image=[UIImage imageNamed:#"girl2.jpeg"];
break;
case 2 :
// setting text to name label
boynamelabel.text=[boysnamearray objectAtIndex:2];
girlnamelabel.text=[girlsnamearray objectAtIndex:2];
// setting text to percent label
boyspercentlabel.text=[boyspercentarray objectAtIndex:2];
girlspercentlabel.text=[girlsprecentarray objectAtIndex:2];
// setting image to image view
boysimage.image=[UIImage imageNamed:#"boy3.jpeg"];
girlsimage.image=[UIImage imageNamed:#"girl3.jpeg"];
break;
default:
break;
}

Related

How can I create one UIViewController with two views that display one or the other depending on button clicked

I'm quite new to iOS development and I am stuck. Currently I am using one tab controller to switch between two view controllers (list and map view). This made it easier to use storyboard to configure the look of the two views.
Now the requirements have changed and the app needs to have one view controller with a segmented control that on click, displays either the list or the map view. In order to do this, I would need to make one view controller that can display list/map view.
I understand how the segmented controller part works, but I'm just stuck on how I can go about having two views with one or the other displayed in the same area.
How can I go about having two views in one view controller (if possible, utilizing storyboard)?
Thanks in advance!
You should not have two main views in a single view controller, instead you need to create one view controller per view that you want to show. However you can certainly have multiple subviews in a single view controller, which may be what works for you.
There are a number of approaches to solve this the problem, the correct approach would be to create a container UIViewController, and add as its childs the 2 viewcontrollers you want to show, them simply set the view to the view controller you want to display, but that would probably be overly complicated since you mention you are new to iOS development.
Therefore an easy solution (not sure if you can implement this in storyboard - since I don't like it), would be to have a single view controller, with the tabs, and 2 subviews of the main view, then you can simply switch between views by doing something like this:
[self.view addSubview:view1];
//to switch
[view1 removeFromSuperview];
[self.view addSubView:view2];
alternatively, you do not really need to remove it from superview but just hide it, and then use bringSubViewToFront to show the view that you need.
If you want to use the other approach I would recommend looking for this video the WWDC 2011 video titled "Implementing UIViewController Containment". This other question should be useful to: UISegmented control with 2 views
Hope that helps.
Using storyboard api you can switch between 2 viewControllers
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *viewController = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
[self addChildViewController:viewController];
viewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:viewController.view];
self.currentViewController = viewController;
}
- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
UIViewController *viewController;
switch (index) {
case 0:
viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
break;
case 1:
viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
break;
}
return viewController;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
UIViewController *viewController = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
[self addChildViewController:viewController];
[self transitionFromViewController:self.currentViewController toViewController:viewController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
[self.currentViewController.view removeFromSuperview];
viewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:viewController.view];
} completion:^(BOOL finished) {
[viewController didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = viewController ;
}];
self.navigationItem.title = viewController.title;
}
This is in reference to iOS tutorial by Raywenderlich. Hope this helps
With Storyboard it is possible in this way.
Create UIViewController with UISegmentControl and UITableView+UITableViewCell added to it.
Now you want to add MKMapView as well, hoverer, if you simply try to place the MapView on the ViewController, it will be added as new TableView cell, which is not what we want.
That's why you should not do it so. Instead, MapView has to be added to Storyboard's List of ViewControllers
Adjust the size and origin of MapView to be the same as TableView ones.
Now, setHidden to YES for either TableView of MapView, create and synthesize outlets for them. Then in Segment control Value Changed method implement switching:
- (IBAction)switchView:(id)sender {
self.theTableView.hidden = !self.theTableView.hidden;
self.theMapView.hidden = !self.theMapView.hidden;
if (!self.theTableView.hidden) {
[self.theTableView reloadData];
}
}

iPhone : Custom tabbar without the awful More menu

So i wanted to build a tabbar that has more than 5 items and is scrollable and found this article.
Easy done by subclassing the UITabBarController and hide the existing tabbar:
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
break;
}
}
Then i just add a UIScrollView and throw some buttons in there based on the items-collection of the tabbarcontroller.
int i = 0;
for (UITabBarItem *item in self.tabBar.items)
{
UIView *tab = [[[UIView alloc] initWithFrame:CGRectMake(i*60, 0, 60, 60)] autorelease];
UIButton *btn = [[[UIButton alloc] initWithFrame:CGRectMake(7.5, 1, 45, 45)] autorelease];
[btn setImage:item.image forState:UIControlStateNormal];
btn.tag = i;
[btn addTarget:self action:#selector(didSelectTabrBarItem:) forControlEvents:UIControlEventTouchUpInside];
[tab addSubview:btn];
[self.scrollView addSubview:tab];
i++;
if(self.selectedViewController == nil)
[self setSelectedIndex:0];
}
I am overriding the setSelectedindex/ViewController since i need some addition drawing.
-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
[super setSelectedViewController:selectedViewController];
[self startTimer];
}
-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
[super setSelectedIndex:selectedIndex];
[self startTimer];
}
The problem is that when I am pressing button number 5, 6 or 7, the tabbarcontroller opens the More view. How do i get rid of that and make the last three items act like the other ones? - Could it be the call to the super?
My guess would be to completely kill the UITabBarController and implement my own custom tabbar. But is it possible to disable the more menu and have the UITabBarController select item 5, 6 and 7 as normal?
So, since I'm too lousy to write a completely new tab bar i decided to investigate and try to hack UITabBarController.
And here's the solution:
The actual problem is that when you touch a tab bar item with index above 4, the UITabBarController vil actually display the moreNavigationController. This is a UINavigationController containing a view of type UIMoreViewControllerList, which is a type from the private Cocoa framework together with an instance of the ViewController you selected.
So how do we get rid of the More button?
Simply remove the UIMoreViewControllerList from the moreNavigationController collection, leaving only the ViewController you selected.
-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
[super setSelectedViewController:selectedViewController];
if([self.moreNavigationController.viewControllers count] > 1)
{
//Modify the view stack to remove the More view
self.moreNavigationController.viewControllers = [[[NSArray alloc] initWithObjects:self.moreNavigationController.visibleViewController, nil] autorelease];
}
}
Well that leaves us with a Edit button in the top right corner (Titlebar).
How do you get rid of that, then?
Yeah. Thats another dirty hack. To remove the Edit button I'd actually have to implement one method from the UINavigationControllerDelegate for the moreNavigationController on my custom UITabBarController.
//navigationController delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController isKindOfClass:NSClassFromString(#"UIMoreNavigationController")])
{
// We don't need Edit button in More screen.
UINavigationBar *morenavbar = navigationController.navigationBar;
UINavigationItem *morenavitem = morenavbar.topItem;
morenavitem.rightBarButtonItem = nil;
}
}
And thats the way to kill off the default More functionality. I really think Apple pissed their own pants here creating a UITabBarController that both handles logic and UI stuff.
Why not create a controller that has the logic to preload the ViewControllers and switch between then, and then an implementation you can use if you want the More thingie. - Or even better: Make it possible to disable the More functionality.
This is pretty close, it may help you
https://github.com/iosdeveloper/InfiniTabBar
I should finalize this thread. I have been having the stuff above in a App Store for a year and it has caused massive problems in the long run. It works, but it quirks when you rely on the built in features of UITabbarcontroller as it messes around with the view stack.
After going around this hot ash for over a year we decided to build our own tabbarcontroller/menucontroller. Thta took like a day and have freed us from all the fixes and quirks.
My hack works, but I recommend building your own navigation class - it will pay off in the long run :-)
I had a similar problem and solved it by using a UITabBarController and hiding the tab bar. Then I drew a custom tab bar over the top of it and when a button for a tab was clicked, call
tabbar.selectedIndex = index
Where 'tabbar' is the original UITabBarController. Setting the selectedIndex property of a UITabBarController changes the currently displayed view controller to the controller at that index. This way you still get all the functionality of a UITabBarController but you can have as many tabs as you want and customize it however you want.
Otherwise, I don't think there is a way to remove the "more" functionality.

Navigation Controller + segment Controller : Unable to change the view

I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basically, for each table view controller I have a dedicated class. To go ahead, I replaced the tab bar controller, and added the segmented controller to the navigation title view.
The question is how can I set the view based on the selected index. I am able to set the navigation title to be segmented control but on select I am unable to set the view.
Below is what i have achieved so far.
Note: What matters is a running code, I would do that code optimization later on. I dont want to hidde views. I want to call different view controller class.
RootViewController class (on click of the button, i call the first view controller. So that I can set the segment controller:
-(IBAction) pushSegmentController: (id) sender
{
NSLog(#"My Location Button being clicked/touched") ;
FirstViewController *firstViewController = [[FirstViewController alloc] init] ;
[self.navigationController pushViewController:firstViewController animated:YES];
// Releae the view controllers
[firstViewController release];
}
IN FirstViewController class:
-(void)viewDidLoad {
[super viewDidLoad];
NSArray *viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Table1", #"Table2"]];
self.navigationItem.titleView = segmentedControl;
[self.segmentedControl addTarget:self
action:#selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
}
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
if(index ==0) {
UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
**???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER**
}
else { }
}
Note: I have tried using this option:
[navigationController setViewControllers:theViewControllers animated:NO];
But this option doesnt give me the right result. How should I go ahead with the same as I want to call a view controller class and set its view based on the selected index.
You probably don't want to have one view controller with different views depending on the button index, especially since you already have view controllers for your different screens.
If you want the table view controller to be pushed onto your navigation controller, so it will have a back button that gets you back to FirstViewController, use
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
UIViewController *newViewController = nil;
if(index ==0) {
newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
} else {
newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain];
}
[self.navigationController pushViewController:newViewController animated:YES];
}
If you'd rather have it slide in from the bottom and you want to handle setting up all necessary user interface (e.g. a dismiss button), replace that last line with
[self presentModalViewController:newViewController animated:YES];
What about?
self.view = table1Controller;
or
[self.view addSubview:table1Controller];
I just saw one more mistake you have done. You are allocating a UIViewController, but initializing it like a tableViewController(with initWithStyle).
If it's a subclass of UITableViewController, alloc it with that, not UIViewController.

Transparent Background with a Modal UIViewController

I have a dilema, I want to present to the user a semi-transparent view.
I found out by experimenting that if I simply pushed the transparent view to the top of my NavigationController's stack, that it would not render the transparency level I wanted. So I decided to simply add the view as a subview of the current view at the top of the stack.
This solution works, the view below is still visible, and the View is 'semi-modal'. The problem is, if the parent view inherits from UITableViewController (as mine does), then the view I 'push' onto it, does not cover the navigation bar at the top.
I really don't want to get into a situation where I am forced to enable / disable controls on the navigation bar every time I push this view, so I was wondering, if anyone knew of any solutions that I could use so that the view I push onto the UITableViewController will actually 'push over' the navigation bar?
Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden.
See this previous question on the topic.
You can still use the view controller and NIB files you have set up - here's my sample code
- (void)showUpgrade {
[self.upgradeVC viewWillAppear:NO];
[self.view addSubview:self.upgradeVC.view];
[self.upgradeVC viewDidAppear:NO];
}
- (void)hideUpgrade {
[self.upgradeVC viewWillDisappear:NO];
[self.upgradeVC.view removeFromSuperview];
[self.upgradeVC viewDidDisappear:NO];
}
- (UpgradeViewController *)upgradeVC {
if (_upgradeVC == nil) {
_upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:#"UpgradeView_%#", self.deviceType] bundle:nil];
_upgradeVC.delegate = self;
}
return _upgradeVC;
}
You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.
It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.
iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.
There is now a way to achieve this using iOS7 custom transitions :
MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];
To create your custom transition, you need 2 things :
A TransitionDelegate object (implementing
<UIViewControllerTransitionDelegate>)
An "AnimatedTransitioning" object
(implementing <UIViewControllerAnimatedTransitioning>)
You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/
Try this:
ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Have you tried looping over the Modal View Controller's subviews and setting the background color to clear for every view? This is a DFS recursive function.
- (void)setBackgroundToClearForView:(UIView *)view {
if ([view subviews]) {
for (UIView *subView in [view subviews]) {
[self setBackgroundToClearForView:subView];
}
}
if ([view respondsToSelector:#selector(setBackgroundColor:)]) {
[view performSelector:#selector(setBackgroundColor:)
withObject:[UIColor clearColor]];
}
}
To use it call:
[self setBackgroundToClearForView:self.view];
in viewDidLoad.
This will do the trick.. Try this one.
// for clear color or you can easily adjust the alpha here
YourVC *vc=[[YourVC alloc]initWithNibName:#"YourVC" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
So that the view will be full screen unlike UIModalPresentationFormSheet..

iPhone view hierarchy question: How do I draw views over the tab bar?

I am having some trouble with view hierarchies and drawing on the iPhone.
To be more specific, I have a tab bar application with a certain tab that contains a table view where I would like the selection of a specific cell to have a UIPickerView slide up. The sliding isn't really a problem (or at least I'm assuming it won't be once I figure this part out), but I cannot seem to get the picker (or any UIView, for that matter) to show up over the tab bar.
I think the way I have this certain tab set up may be the problem. Normally, in any other tab, I could just do something like this:
[self.tabBarController.view addSubview:pickerView];
and everything would work fine.
But for this specific tab, I have a UISegmentedControl in the navigation bar that switches between two different UITableViews. So the view controller associated with the tab (call it TabViewController) has its own instances of these two table view controllers (TableOneViewController and TableTwoViewController) and will insert the currently selected table view (based on the segmented control) as a subview of TabViewController's view.
If I didn't need to switch the views like this I could just call
[tabViewController.tabBarController.view addSubview:pickerView];
from TabViewController and the picker would show up over the tab bar. But the thing is I cannot call this in either of the table view controllers selected within this tab (well I can, but it doesn't do anything). I have tried passing this tabBarController property into the table view controller, but that doesn't work either. I have also tried messing around with the app delegate (which I'm trying to avoid) to no avail.
Is there something simple I'm missing here, or can this not be done? I feel like it should since a keyboard can slide up over the tab bar in this table view. Is there a way to just draw over all the current views and subviews?
Here is what is called when the segmented control is selected within TabViewController.m, and switches the views:
- (IBAction)switchViews:(id)sender
{
if (self.tableOneViewController.view.superview == nil)
{
if (self.tableOneViewController == nil)
{
TableOneViewController *tempOneController = [[TableOneViewController alloc] initWithNibName:#"TableOneViewController" bundle:nil];
self.tableOneViewController = tempOneController;
[tempOneController release];
}
[tableTwoViewController.view removeFromSuperview];
[self.view insertSubview:tableOneViewController.view atIndex:0];
}
else
{
if (self.tableTwoViewController == nil)
{
TableTwoViewController * tempOneController = [[TableTwoViewController alloc] initWithNibName:#"TableTwoViewController" bundle:nil];
self.tableTwoViewController = tempOneController;
[tempOneController release];
}
[tableOneViewController.view removeFromSuperview];
[self.view insertSubview:tableTwoViewController.view atIndex:0];
}
}
And here's what's going on when I try to add the picker in TableOneViewController.m:
UIPickerView *tempPicker = [[UIPickerView alloc] init];
tempPicker.delegate = self;
tempPicker.dataSource = self;
tempPicker.showsSelectionIndicator = YES;
tempPicker.clipsToBounds = NO; // thought this might work, but doesn't
self.picker = tempPicker;
[tempPicker release];
[self.view addSubview:pickerPicker]; // adds beneath tab bar!
[[[UIApplication sharedApplication] keyWindow] addSubview:someViewController];
Make sure the viewController you are loading in is 480px high!
To remove it again, use [someViewController removeFromSuperview];
Maybe you want to do just
[tabBarController presentModalViewController:someViewController animated:YES];
?
This should slide on top of anything else you have on the screen.