Use segmented Control to switch between 2 views - iphone

I make an iphone application that use a segmented Control to switch between 2 viewControllers that display different informations. So, I defined in the first view Controller a segmented Control that I linked in IB to a Segmented Control that I place on the corresponding view.
#interface FirstViewController : UIViewController{
//NSArray * viewControllers;
//UINavigationController * navigationController;
IBOutlet UISegmentedControl *segment; //->segment linked in the nib of FirstViewController
}
The action related to the segmented control is the following:
-(IBAction)valuechanged:(id)sender{
NSInteger index = [(UISegmentedControl *)sender selectedSegmentIndex];
UIViewController *parking=[[ParkingViewController alloc]
initWithNibName:#"ParkingViewController" bundle:nil] ;
viewControllers= [NSArray arrayWithObjects:self,parking,nil];
if(index==1){
UIViewController * incomingViewController = [viewControllers objectAtIndex:index];
[self presentModalViewController:incomingViewController animated:YES];
}
}
In this Action, I define what to do once user click on segmented control.Here, it's loading a new viewController named ParkingViewController. The problem is that once the new ParkingViewController is loaded the segmentedControl disappear and so I can't come back to the firstViewController.
I don't know how to do to keep the segmentedControl for both views?
Thank u all
Quentin

Generally, the UITabBarController is used to control switching between two or more view controllers. It will stay on the screen and allow switching back and forth.
The segmented controller you have cannot stay on the screen when you present a modal view. A modal view will take over the entire screen. Also, because you are animating it onto the screen, a copy of the segmented view controller in the new view will not appear to be the same segmented control because the user saw it scroll onto the screen.

I can't come back to the firstViewController... ??
You can add a UIButton and call an IBAction
[self dismissModalViewControllerAnimated:NO];

Related

UITabBar MoreViewController image in UITableView disappears after programmatically selecting it

I'm trying to select one of my UINavigationControllers in the UITabBar.viewControllers array.
I previously tried it with setting the UITabbarController.selectedIndex, but the Apple documentation says:
"To select the More navigation controller itself, you must change the value of the selectedViewController property instead."
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UINavigationController *navController = [appDelegate.objTabBarController.viewControllers objectAtIndex:5];
[appDelegate.objTabBarController setSelectedViewController:navController];
Doing so is fine but when I change back to the MoreViewController's list view, the icon on the left side is gone and won't come back.
Everything ok, when selecting it with the finger.
Bug when selecting programmatically -> image is gone
Any suggestions what I'm doing wrong?
Best regards,
Steve
Selecting the More Navigation Controller
To select the moreNavigationController, you must set the selectedViewController:
self.tabBarController.selectedViewController = self.tabBarController.moreNavigationController;
The reason for that is simple: The moreNavigationController is not inside the viewControllers of the tab bar controller, so it can't be selected by index.
To select one of the "more" controllers directly, you can either set the selectedViewController property:
self.tabBarController.selectedViewController = viewController5;
Or you can set the selectedIndex:
self.tabBarController.selectedIndex = 5;
Fixing the Disappearing Tab Bar Image
The disappearing tab bar image is caused by using a navigation controller (for the settings) inside a navigation controller (the moreNavigationController generated by the tab bar controller). To fix it, there are two solutions:
Don't add navigation controllers for the more section, but add the controllers directly. The controller structure would look like this, assuming the controllers in all tabs need navigation:
tabBarController
+ navigationController
+ viewController0
+ navigationController
+ viewController1
+ navigationController
+ viewController2
+ navigationController
+ viewController3
+ viewController4
+ viewController5
Set the tabBarItem of the settings controller on its navigation controller (you only need to do this once):
UINavigationController *settingsNavigationController = [appDelegate.objTabBarController objectAtIndex:5];
UIViewController *settingsRootController = settingsNavigationController.viewControllers[0];
settingsNavigationController.tabBarItem = settingsRootController.tabBarItem;
I ran into this same problem. It seems that the navigation controller was somehow losing track of the view controller. In my case, the navigation controller whose image was disappearing was at index 7 in the tabBarController. The navigation controller was supposed to contain a viewController of class SettingsViewController. But sometimes it lost it.
To fix it, I had to add code to two classes -- the app delegate and SettingsViewController. In the app delegate:
-(void) fixSettingsNavigationController {
UITabBarController* tab = self.tabBarController;
NSArray* vcs = tab.viewControllers;
NSInteger nVCs = vcs.count;
if (nVCs > 7) {
UIViewController* settingsContainer = vcs[7];
UINavigationController* settingsContainerNav = (UINavigationController*)settingsContainer;
if ([settingsContainerNav isKindOfClass: [UINavigationController class]]) {
NSArray* settingsNavVCs = settingsContainerNav.viewControllers;
NSInteger count = settingsNavVCs.count;
SettingsViewController* svc = self.settingsViewController;
if (!count) {
// The container navigation controller has lost track of the settings view controller.
settingsContainerNav.viewControllers = [NSArray arrayWithObject:svc];
}
}
}
}
And in SettingsViewController:
-(void) viewWillDisappear:(BOOL)animated {
MyAppDelegate* appDel = [[UIApplication sharedApplication] delegate];
[appDel fixSettingsNavigationController];
[super viewWillDisappear: animated];
}
I ran to the very similar situation, where I want to select a View controller inside MoreViewController programmatically.
As you said in your question, apple don't allow selecting a view controller that is inside MoreViewController (Or not visible in tabbar).
In my case, I don't need default "More" features of reordering tabs in tabbar.
So, I did following things
Created a UITableViewControlelr as my 5th tab, set its Title to more and custom image (Similar to default more image).
In my UITableViewController I listed items that corresponds to ViewControllers. Tapping on which show respective view controller.
When i need to show any view controller that is inside my UITableViewController programatically, i simply set a flag and push my UITableViewController, and in viewDidAppear method of my UITableViewController push desired view controller.
This will also show back button on programatically opened view to list of MoreViewController

iDev Custom Tab Bar Modal Window being Hidden by Tab Bar. How can I change this?

I have been attempting to present a modal view from one of my views that I implemented following the following iDev tutorial/source code. The custom tab bar manages view by inserting them as subviews beneath the tabBar so when I call the modal view to be displayed from the subview it is covered up at the bottom by the tab bar. I have switched the following code to above the tab bar sub view which allows the whole screen to be displayed but it still creates animation problems.
// Set the view controller's frame to account for the tab bar
viewController.view.frame = CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height-(tabBarGradient.size.height));
// Set the tag so we can find it later
viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
// Add the new view controller's view
[self.view insertSubview:viewController.view aboveSubview:tabBar];
http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/
Please help this is driving me nuts!
So, i think you are presenting modal view from your subview like this:
[self presentModalViewController:someController animated:YES];
If so, your controller is presenting on current view (self) and if current view situated beneath other views - modal controller will be presented under them too.
You should get a reference to main parent view, so you could call presentModalViewController there.
In big projects with many subviews it's handy to have a reference to main view, I did it like this:
in MyAppDelegate #interface I have:
{
UINavigationController *navController;
}
+ (UINavigationController *) navController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
And in the implementation:
// Used for showing modal windows over tabbar
static UINavigationController * _rootController = nil;
+ (UINavigationController *) navController {
return _rootController;
}
#synthesize navController;
in didFinishLaunchingWithOptions:
_rootController = navController;
(In this example my top view - navigation controller)
So anywhere in application I can call:
[[MyAppDelegate navController] presentModalViewController:someController animated:YES];

What is the proper way to access navbar buttons of a modal view?

I created a view in IB with a navbar and a table. On the navbar I put two buttons, cancel and done. I use this view like a modal view with:
[self presentModalViewController:controller animated:YES];
My problem is when I use:
[self.navigationItem.rightBarButtonItem setEnabled:YES];
to enable the right button. It doesn't work.
Have I to set a delegate? what code passages I have to do? It works if I create an IBOutlet for the right button and I use [doneButton setEnabled:YES], but I think this isn't the proper way.
In order to place a navigation bar on your modal view controller in interface builder (and set up bar button items that call actions in your detail view controller), you need to go through a level of indirection (your navigation bar will be in one .xib, and the details of your detail view will be in a different xib):
create a xib file containing a navigation controller object, and set its root view controller to be your detail view controller that you want to display modally with a navigation bar.
add bar button items to the detail controller's navigation bar and hook them up to IBActions in your detail view controller object.
your detail view controller will need to be in a separate .xib file
create a "loader" object that just exists to hold the navigation controller iboutlet, and set it to be the File's Owner object of that xib:
#interface Loader : NSObject
#property (nonatomic, retain) IBOutlet UINavigationController *navVC;
#end
#implementation Loader
#synthesize navVC;
- (void) dealloc
{
[navVC release];
[super dealloc];
}
#end
Your xib file containing the navigation controller will look like this:
Make sure the navigation controller object is conntected to the "Loader" object's navVC outlet, and make sure the bar button items are connected to your detail view controller's desired IBActions.
Then you present this whole thing using this code:
Loader *loader = [[[Loader alloc] init] autorelease];
[[NSBundle mainBundle] loadNibNamed:#"ModalVC" owner:loader options:nil];
[self presentModalViewController:loader.navVC animated:YES];
Delegate has nothing to do with your issue.
You probably did put navigation bar into your view directly. Thus things like self.navigationItem doesn't work. You have two choices ...
Connect your buttons to outlets in your code and access them directly.
Or remove navigation bar from your view and present your view controller in this way ...
MyViewController *vc = [[MyViewController alloc] initWith...];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
[vc release];
[self presentModalViewController:navCon animated:YES];
[navCon release];
... and now you can access left/right buttons via navigationItem.

Pop-up modal with UITableView on iPhone

I need to pop up a quick dialog for the user to select one option in a UITableView from a list of roughly 2-5 items. Dialog will be modal and only take up about 1/2 of screen. I go back and forth between how to handle this. Should I subclass UIView and make it a UITableViewDelegate & DataSource?
I'd also prefer to lay out this view in IB. So to display I'd do something like this from my view controller (assume I have a property in my view controller for DialogView *myDialog;)
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"DialogView" owner:myDialog options:nil];
myDialog = [nibViews objectAtIndex:0];
[self.view addSubview:myDialog];
problem is i'm trying to pass owner:myDialog which is nil as it hasn't been instantiated...i could pass owner:self but that would make my view controller the File's Owner and that's not how that dialog view is wired in IB.
So that leads me to think this dialog wants to be another full blown UIViewController... But, from all I've read you should only have ONE UIViewController per screen so this confuses me because I could benefit from viewDidLoad, etc. that come along with view controllers...
Can someone please straighten this out for me?
There is no such thing as a view controller being on the screen; its view is on the screen. With that said, you can present as many views as you want on the screen at once.
I would create a new view and view controller. You would not make a UIView be a UITableViewDelegate, you make a UIViewController be a UITableViewDelegate. But instead of doing that manually, instead make your new view controller a subclass of UITableViewController, if you're using iPhone OS 3.x+. You can then present this view controller modally.
You probably want to give the user a chance to cancel out of the selection. A good way to do that is to wrap your new dialog view controller in a UINavigationController and then put a "Cancel" button in the nav bar. Then use the delegate pattern to inform the parent view controller that the user has made their choice so you can pop the stack.
Here's what the code will look like inside your parent view controller, when you want to present this option dialog:
- (void)showOptionView
{
OptionViewController* optionViewController = [[OptionViewController alloc] initWithNibName:#"OptionView" bundle:nil];
optionViewController.delegate = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:optionViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[optionViewController release];
}
Your OptionViewController .h will look like this:
#protocol OptionViewControllerDelegate;
#interface OptionViewController : UITableViewController
{
id<OptionViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id<OptionViewControllerDelegate> delegate;
#end
#protocol OptionViewControllerDelegate <NSObject>
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSString*)selection;
// or maybe
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection;
// etc.
#end
Your OptionViewController.m will have something like this:
- (void)madeSelection:(NSUInteger)selection
{
[delegate OptionViewController:self didFinishWithSelection:selection];
}
Which has a matching method back in your original view controller like:
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection
{
// Do something with selection here
[self.navigationController dismissModalViewControllerAnimated:YES];
}
There are plenty of examples throughout Apple's sample source code that follow this general pattern.

iPhone - tabbar + view issue

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