Showing Master/Detail UITableView inside UIPopOverController - iphone

I have a UIPopOverController that shows a UIViewController with a UITableview in its view. The cells in the table have a detailedView, but whenever that view gets pushed, the PopOverController increases in size, and I am left with all this white space inside it.
Question is this: Can anyone show me how I can have a Master/Detail UITableview show inside a PopOverController whilst preserving its dimensions?
Some of my code if it helps you:
//Creating the PopOver with the UIViewController
addTaskViewController = [[AddTaskViewController alloc] initWithNibName:#"AddTaskViewController" bundle:nil];
UINavigationController *addTaskNavController = [[UINavigationController alloc] initWithRootViewController:addTaskViewController];
UIPopoverController *addTaskPopOver = [[UIPopoverController alloc] initWithContentViewController:addTaskNavController];
self.addTaskPopOverController = addTaskPopOver;
addTaskPopOverController.delegate = self;
//...neccessary releases...
//Showing the popover when a button is pressed
- (void) addTasksButtonPressed:(id)sender {
//Display the Popover containing a view from AddTaskViewController
[self.addTaskPopOverController setPopoverContentSize:CGSizeMake(400, 700)];
[addTaskPopOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

You should set the detail view controller's contentSizeForViewInPopover property to the same value as the parent controller.

Related

Moving to next view in popover displaying UITableView

I have created an application for iPhone interface which displays a UITableView as its rootviewcontroller. When we select any of its row, it opens the corresponding detailViewController, we select the value from there and it is displayed in the cells of UITableView in masterViewController. This application was running perfectly fine in iPhone simulator as desired.
Now I want to incorporate this application to an iPad application. For that purpose, I have created a UIPopOver and assigned the masterViewController to the popover. Now the problem is, UITableView is initially shown in the popover. But when we select any row, next view doesn't appear in popover as it appeared when application was running solely for iPhone. How should I make it work so that I can import the application with original functionality in UIPopOver?
I think you are pushing DetailViewController from your masterViewController.But your masterviewController should implement NavigationController, I mean while displaying masterViewController(your tableviewcontroller) in popover you should do as follows:
MasterViewController *theMasterViewController = [[MasterViewController alloc] init];
UINavigationController *navCont = [[UINavigationController alloc] initWithRootViewController:theMasterViewController];
[theMasterViewController release];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:navCont];
popOverController.popoverContentSize = CGSizeMake(400, 400);
[popOverController presentPopoverFromRect:btn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
and when user selects any row, then in didSelectRowAtIndexPath do as follows:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
Hope this helps!

UISegmentedControl in a PopoverController with multiple view controllers

I would like to have a UISegmentedControl embedded in a PopoverController, similar to what is described in this SO question : UISegmentedControl embedded in a UINavigationBar/Item
The difference is that I have a different view controller for each view that I want to show in the popover, depending on the selected index on the Segmented Control. I'm not sure how I would go about doing this. Whenever I try to push a new view on top of the root view controller, the UISegmentedControl disappears. I would just like to switch between the two viewcontrollers, while keeping the UISegmentedControl visible. Is this even possible?
Thanks in advance!
If its a different viewController for each one of the segments on the segmentBar, you'll have to use a container viewController that adds the views of each of the viewController as a subview on itself or sets its view to that of the viewController's view. For example:
UIViewController* containerController = [[[UIViewController alloc] init] autorelease];
//Inside the viewDidLoad of the the ContainerController class, do the following:
//Initialize all three viewControllers
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
//set up the segment and add it to the container's navBar's title view.
[segmentedControl addTarget:self action:#selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
- (void)segmentValueChanged:(id)sender
{
//if first tab selected
[self.view removeAllSubviews];
[self.view addSubview:test1.view];
//if second tab selected
[self.view removeAllSubviews];
[self.view addSubview:test2.view];
//if third tab selected
[self.view removeAllSubviews];
[self.view addSubview:test3.view];
}
Instead of adding it as a subView, you might be able to just set self.view = test1.view. Obviously, you would use the container view to initialize the navController and put that navController inside the popover. Hope this helps!
If you are using presentModalViewController method to show your new view controller on the screen, it will always cover the entire screen and what ever is underneath it. That's just how it works.
As per docs:
On iPhone and iPod touch devices, the view of modalViewController is
always presented full screen. On iPad, the presentation depends on the
value in the modalPresentationStyle property.
The way to do it and still being able to control how the view controller is positioned is to create your own presentation method.

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.

Having a UITabBar while displaying a view not on the UITabBar?

I have been having some trouble with the UITabBar and cannot find anything online in regards to the subject. Pretty much I want to display a view (say TestView) and a TabBar that contains to other views: Green View and Red View. So initially I want TestView displayed with a tabBar on the bottom with 2 tabs, one for Green view and one for Red View, once one of those tabs are touched the appropriate view will be displayed, but there will be no tab for the TestView
But heres the MainAppDelegate code and a picture of what I get
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Create the TabBar VC and ButtonSelect VC
testView = [[TestViewController alloc] init];
tabBarController = [[UITabBarController alloc] init];
//Create the viewcontroller's For the TabBarController
UIViewController *gvc = [[GreenViewController alloc] init];
UIViewController *rvc = [[RedViewController alloc] init];
//Make a array to containing the two viewcontrollers (for TabBar)
NSArray *viewControllers = [NSArray arrayWithObjects:gvc, rvc, nil];
//Attach the VC's to the TabBar
[tabBarController setViewControllers:viewControllers];
//Set to window
[window addSubview:[tabBarController view]];
[window addSubview:[testView view]];
[window makeKeyAndVisible];
[rvc release];
[gvc release];
return YES;
}
And This is what I get..
Please help
Adjust the frame of your testView. For example,
testView = [[TestViewController alloc] initWithFrame: CGRectMake(
0.0, 20.0, 320.0, 421.0)];
I used "magic numbers" here, but you had better get the height of the status bar and tab bar.
to the comment
So you want to dismiss the test view when the user selects one of the tab. You can catch the moment when a tab is selected by setting a delegate to the UITabBarController and implement tabBarController:didSelectViewController:` method. Basically, you will dismiss the test view in this method.
One way of doing it (and without changing your code much) is to set your application delegate as the delegate of the UITabBarController, (tabBarController.delegate = self;). Then implement tabBarController:didSelectViewController:, where you remove the test view from your application window and do tabBarController.delegate = nil;.

Using a view controller both as modal view controller and as a tab bar view controller

I have a view controller alike contacts in iPhone. The code is something like this,
tabBarController = [[UITabBarController alloc] init];
friendsVC = [[RemittanceFriendsVC alloc] initWithNibName:#"RemittanceFriendsView" bundle:nil];
friendsVC.friendsArray = [[RemittanceModel getInstance] friends];
UINavigationController *friendsNVC = [[UINavigationController alloc] initWithRootViewController: friendsVC];
[controllers addObject:friendsNVC];
tabBarController.viewControllers = controllers;
The RemittanceFriendsVC is UITableViewController, clicking on a cell takes to details view. I have 'modal' variable set in the ViewController (VC)to know if its loaded as modal or not. Since its part of a tab bar item, (non modal view) it works fine. But when I am loading it as modal VC, when I click on a table cell, I want to dismissmodalview, but it did not dismiss the modal view.
In the friendVC this is not working,
-(void) didPressCancelButton {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
What I wanted to do is, use the same VC as a tab bar item and sometime as a modal VC. Isn't it possible?
okay, it was the problem with the
[self.navigationController dismissModalViewControllerAnimated:YES];
it should be,
[self dismissModalViewControllerAnimated:YES];
Then it works fine.