I have seen the post for How to switch views by buttons on iPhone? but this doesn't answer how to switch back and forth between views with buttons. The person that asked the question settled on the answer that they could switch between views with uinavigationcontroller.
I put the following code in an ibaction that kicks off when a button is pressed in the primary view.
PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:#"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];
When this code executes the whole view just goes blank. If I omit the removefromsuperview portion then the view disappears behind my button but the button still remains. I'm not sure if this is the right way to switch between buttons but if anyone knows how to do this please help. Also if anyone knows about any example projects that switch between views with buttons let me know.
Thanks a million!
You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.
You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.
// origView is an instance variable/IBOutlet to your original view.
- (IBAction)switchToPhoneView:(id)sender {
if (origView == nil)
origView = self.view;
self.view = phoneViewController.view;
}
- (IBAction)switchToOriginalView:(id)sender {
self.view = origView;
}
The technique I usually use involves creating a superview class which contains a toolbar at the bottom, and a content view UIView class filling the rest of the screen.
I then add subviews to the content view to change the views based on button clicks. This approach makes it so the toolbar on the same is constant across all views. I start by defining a helper function like this:
-(void) clearContentView {
//helper to clear content view
for (UIView *view in [self.contentView subviews]){
[view removeFromSuperview];
}
}
My IBAction then looks like this:
-(IBAction) buttonClicked{
self.title = #"Images"; //change title of view
[self clearContentView]; //clear content view
[self.contentView addSubview:self.imagesViewController.view]; //add new view
[self.imagesViewController viewWillAppear:YES]; //make sure new view is updated
[self enableButtons]; //enable all other buttons on toolbar
self.imagesButton.enabled = NO; //disable currently selected button
}
Related
I am using a UIView item on my view controller that contains a picker view and a button, which needs to appear on the screen only when the show button is clicked.
I created a outlet for my UIView with name *pickerView
The default position of this view (on the right properties bar of Xcode) is (0,200,320,261) for (x,y, height and width) which basically makes it appear at the base of the ViewController.
What I did for this view to hide initially when the view controller loads is, in the ViewDidLoad method I put this code:
pickerView.frame=CGRectMake(0,450,320,261);
For the action of show button,
pickerView.frame=CGRectMake(0,200,320,261);
I have a hide button inside this UIView, in its action i have
pickerView.frame=CGRectMake(0,450,320,261);
SO, from what I expect when I run the application, the UIView pickerView should initially hide because of code in viewDidLoad, and show button should bring it on the screen.
My problem is show and hide button works fine, but every time I load this ViewController the View appears on screen by default. Help me hide this UIView when I load the viewController.
Simpley don't set the frame for picker view
i am posting a sample code it makes picker view hide and appear when tapping the button
i am using the property "hide"
hear is the sample code
- (void)viewDidLoad
{
[super viewDidLoad // Do any additional setup after loading the view, typically from a nib.
//as simple dont set frame.
// i am using xib from there i wired up picker view
self.myPickerView.hidden = YES; //just hide it whenever you dont use it.
}
- (IBAction)whenShowHideButtonTapped:(id)sender
{
//when button pressed just show it
if(self.myPickerView.hidden)
{
self.myPickerView.hidden = NO;
}
else
{
self.myPickerView.hidden = YES;
}
}
hope this helps .. :)
You could call the hide code in 'viewWillAppear:' like this:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hidePicker];
}
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];
}
}
I want to display a UIViewController as a small popover over an other UIViewController.
The UIViewController should be display modal, but not take the whole screen.
It should just show up on a lower third of the screen...
Can somebody point me to a tutorial or give me some hints to start on this?
I googled for over one hour, but could not find anything, that helps me :(
Thanks for your help,
Stefan
Just discovered that this question is still unanswered ... You have multiple options to achieve such a thing running on iPhone:
Depending on the situation, I think I would just go with a UIViewController subclass loading from a NIB that you configured to show a screen-sized view with the backgroundColor property set to [UIColor clearColor]. Add whatever subviews you want to display as a popover to that view at the position you want (e.g. lower third of screen). You can then present the view controller modally with the UIModalTransitionStyleCrossDissolve to fade it in and even use the background view to intercept touch events to dismiss the modal view.
Another possibility would be to just add the popover's view controller's view as a subview to the main view controller's view. You can use UIView's animation class methods to animate the transition.
this might help.
you have to declare popOverController in the header and make it a property as well as synthesize it.
if([self.popOverController isPopoverVisible])
{
[self.popOverController dismissPopoverAnimated:YES];
return;
}
UINavigationController *favNav = [[UINavigationController alloc]
initWithRootViewController:favoritesView];
//favoritesView is an outlet to the VC Favorites
//make a nav controller with the root view an outlet to the view you want to present.
self.popOverController = [[[UIPopoverController alloc]
initWithContentViewController:favNav] autorelease];
[popOverController presentPopoverFromBarButtonItem:revealFavorites permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //revealFavorites is the button i press to show the favorites popover
favoritesView.view.frame = CGRectMake(10, 10, 310, 320); //set the frame
if (![self.popOverController isPopoverVisible]) {
[favNav release];
}
}
I am using a UITabBar control from library in one of my view (note that I am not using UITabBarController but the UITabBar control).
Now, I am adding two tabBar items to this tabBar.
I have created controller class for this view (.m and .h) files and used delegates in the .h file.
In the .m file I have used the following function:
(void)tabBar:(UITabBar *)TabBarControl didSelectItem:(UITabBarItem *)FirstView
I have assigned tag = 0 and tag = 1 to respective tabBar items.
What I want to do is that, on click of first tabBar item I want to show a view and click of another tabBar item, I want to show another view.
So, in the above function I am checking that if the tag of clicked tabBar item is 0 than I will show one view else I will show another view.
I am showing the view as following:
Team1Scoreboard *tempTeam1Scoreboard = [Team1Scoreboard alloc];
tempTeam1Scoreboard = [tempTeam1Scoreboard initWithNibName:#"UserTeamScoreboard" bundle:[NSBundle mainBundle]];
self.cntrlTeam1Scoreboard = tempTeam1Scoreboard;
[tempTeam1Scoreboard release];
UIView *theWindow = [self.view superview];
[self.view removeFromSuperview];
[theWindow addSubview:self.cntrlTeam1Scoreboard.view];
Now the problem is that, when I click on any of the tabBar item, it will load the correct view but the tabBar itself will be disappeared as I am adding the view to window itself.
Please help me so that I can load correct view and also my tabBar itself is visible.
The TabBar is disappearing because it's a child of the view which you are then adding a new child to and the new child is sized the same as the parent. Did that make sense? Ok, look at it this way:
You have ViewA and ViewA has a couple of labels and a TabBar. ViewA is managed by ViewControllerA. In ViewControllerA you are creating an instance of ViewB and calling ViewControllerA.view addSubView:instanceOfViewB, right? Before doing that, you will want to resize ViewB.
Try something like this:
ViewControllerB *viewControllerB = [[ViewControllerB alloc]initWithNibName:#"ViewB" bundle:nil];
CGRect frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 40);
viewControllerB.view.frame = frame;
[self.view addSubview:viewB.viewControllerB];
Basically it should be close to what you are doing, but I'm setting the size to be 40 px less (whatever you need to remove 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.