Master Detail Application for iPad - ios

I am creating an iPad version of Master Detail Application using XCode 4.5 with ARC.
I have my iPadMaster.h/.m(as my master) and iPadDetailViewController.h/m(as my detail) set up.
I am trying to load different view controllers from iPadDetailViewController when users click/select the rows on iPadMaster.
I set this on iPadMaster.m at:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
iPadDetailViewController * DVC = [[iPadDetailViewController alloc]initWithNibName:nil bundle:nil];
DVC.itemNumber = indexPath.row;
}
and tried this stupid stunt on iPadDetailViewController.m on [viewDidLoad]:
switch(_itemNumber)
{
case 0:
{
//Detail row
vc1 *viewController = [[vc1 alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
break;
}
case 1:
{
//Report row
vc2 *viewController = [[vc2 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
case 2:
{
//Report row
vc3 *viewController = [[vc3 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
...
case 9:
{
//Report row
vc9 *viewController = [[vc9 alloc] init];
//viewController.somePassedInData = theDataToPass;
[self presentViewController:viewController animated:YES completion:nil];
break;
}
default:
{
break;
}
On iPhone i would just plaster the 'switch cases' in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath but i'm at lost with iPad environment...
Thanx in advance...

The split view controller has a property, viewControllers. The object at index 1 is the detail controller. You should just create a mutable copy of viewControllers, replace the object at index 1 with your new controller, and set that array to be the split view's arrayControllers.
NextController *next = [[NextController alloc] init..... // or however you get your new controller
NSMutableArray *mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;

Related

How to link UITableView Cell to different ViewControllers in Storyboard

I have created a UITableView and set everything up so that it pulls the correct Data that I want on the table. The only issue I have is that when I click on an item, I want it to open that viewController
I have set the Storyboard ID up correctly so everything should work but please see my code below incase there's something obvious I've missed:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
UIViewController *newTopViewController;
if (indexPath.section == 0) {
NSString *identifier = [NSString stringWithFormat:#"%#", [self.section1 objectAtIndex:indexPath.row]];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
} else if (indexPath.section == 1) {
NSString *identifier = [NSString stringWithFormat:#"%#", [self.section2 objectAtIndex:indexPath.row]];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
else if (indexPath.section == 2) {
NSString *identifier = [NSString stringWithFormat:#"%#", [self.section3 objectAtIndex:indexPath.row]];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
}
I can provide more info if needed
You use instantiateViewControllerWithIdentifier to create view controller objects.
But you are not presenting the created view controller object so add:
[self.navigationController presentViewController:newTopViewController animated:YES completion:nil];
If Identifiers are correct and you set segues in your storyboard then you can just call
[self performSegueWithIdentifier:indentifier sender:nil];
You need to either Push yournewTopViewController
[self.navigationController pushViewController:newTopViewController animated:YES];
Or Modally Present your newTopViewController
[self.navigationController presentViewController:newTopViewController animated:YES completion:nil];
in the end of your didSelectRowAtIndexPath Method.
To answer your question I believe you should first import all your .h files First.
Then you can go
if (indexPath.section == 0) {
FirstViewController *firstController = [[FirstViewController alloc] init];
[self presentViewController:firstController animated:YES completion:nil];
}
And so on and so forth

Unable to load FBFriendPickerViewController using Storyboard

I am making an replica of Scrumptous sample app (ios facebook sdk version - 3.10 ) using Storyboard approach
I am able to load user name and profile image correctly.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do nothing for now
switch (indexPath.row) {
case 2:
if (!self.friendPickerController) {
self.friendPickerController = [[FBFriendPickerViewController alloc]
initWithNibName:nil bundle:nil];
// Set the friend picker delegate
self.friendPickerController.delegate = self;
self.friendPickerController.title = #"Select friends";
}
[self.friendPickerController loadData];
[self.navigationController pushViewController:self.friendPickerController
animated:true];
break;
}
}
With below approach we can instantiateViewController but we need a valid identifier
/*
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"myViewController"];
[self presentViewController:vc animated:YES completion:NULL];
*/

Master Detail Application not loading UIViewControllers

I am creating an iPad version of Master Detail Application using XCode 4.5 with ARC. I have my iPadMaster.h/.m(as my master) and iPadDetailViewController.h/m(as my detail) set up.
I am trying to load different view controllers from iPadDetailViewController when users click/select the rows on iPadMaster.
On iPadDetailController.h, i set this:
#property int itemNumber;
On iPadMaster.h, i called it as:
#class iPadDetailViewController;
and proceeded with this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController * DVC = [[DetailViewController alloc]init];
DVC.itemNumber = indexPath.row;
}
On iPadDetailViewController, i set this:
- (void)configureView
{
switch (_itemNumber) {
case 1:
{
iPadLogin *next = [[iPadLogin alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;
break;
}
default:{
self.view.backgroundColor = [UIColor whiteColor];
}
break;
}
}
//then i called it on:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
When i clicked the second row in the master table, item_number should be 1 and load the 'iPadLogin' but nothing happens... Any pointers is much appreciated...
Thanx in advance...
As I said in my comment, I think you should change the detail controller from the master controller. It is in the master that you're making the decision on what detail controller to go to (by picking a row in the table), so it should be the responsibility of the master controller to make the change. The code below should do that (note, however, if you're using a storyboard for your controllers, then you should be using [self.storyboard instantiateViewControllerWithIdentifier:#"whatever"] to get your next controller rather than alloc init).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 1:
{
iPadLogin *next = [[iPadLogin alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:next];
self.splitViewController.viewControllers = mut;
break;
}
case 2:
{
AnotherVC *another = [[AnotherVC alloc] init];
NSMutableArray *mut = [[NSMutableArray alloc]init];
mut = [self.splitViewController.viewControllers mutableCopy];
[mut replaceObjectAtIndex:1 withObject:another];
self.splitViewController.viewControllers = mut;
break;
}
default:{
UIViewController *detail = self.splitViewController.viewControllers[1];
detail.view.backgroundColor = [UIColor whiteColor];
}
break;
}
}

iOS table navcontroller not working

I have a table:
- (void)viewWillAppear:(BOOL)animated
{
self.searchBar.delegate=self;
self.searchResultTable.delegate=self;
self.searchResultTable.dataSource=self;
[self.searchResultTable setHidden:YES];
}
In my cells I insert text and when I click on cell I need go to another table but this not work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewController *tvc=[[TableViewController alloc]initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:tvc animated:YES];
}
Assuming you are doing something like this to present this controller:
SearchViewController *searchViewController = [SearchViewController alloc] init];
[self presentModalViewController:searchViewController animated:YES];
then your view controller doesn't have a navigation controller
You need to change it to
SearchViewController *searchViewController = [SearchViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
[self presentModalViewController:navController animated:YES];
then [self.navigationController pushViewController:tvc animated:YES]; will work

Updating DetailViewController from RootController

I'm trying to create an iPad application with a similar user interface to Apple's Mail application, i.e:
RootView controller (table view) on the left hand side of the split view for navigation with a multiple view hierarchy. When a table cell is selected a new table view is pushed on the left hand side
The new view on the left side can update the detail view.
I can accomplish both tasks, but not together.
I mean I can make a multi-level table view in the RootController.
Or I can make a single-level table view in the RootController which can update the detailViewController.
Can anyone tell me how to make a multi-level table in the RootController which can update a detailViewController?
There is more source code at the link but below is the method in which I presume I have to declare a new detailViewController (which has to be put in the UISplitViewController):
- (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:#"Children"];
//
if([Children count] == 0) {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc]initWithNibName:#"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc]initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers//nothing happens.....
[viewControllers release];//
}
else {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc]initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.current_level += 1;
//Set the title;
rvController.current_title = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController.tableView reloadData]; //without this instrucion,items won't be loaded inside the second level of the table
[rvController release];
}
}
Sorry, but I cannot post my source code as it contains sensitive information. When I have more time available I will create a separate project and upload the code somewhere.
Here are extracts of how I have done it so far (I welcome any feedback).
The RootViewController - Note I have 4 sections in my root table.
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Detail view logic
NSUInteger section = indexPath.section;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (section == 2) {
ProductSearchDetailView *viewController = [[ProductSearchDetailView alloc] initWithNibName:#"ProductSearchDetailView" bundle:nil];
detailViewController = viewController;
//[viewController release];
}
else {
DetailViewController *defaultDetailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailViewController = defaultDetailViewController;
//[defaultDetailViewController release];
}
// Navigation logic
switch (section) {
case 0:
{
break;
}
case 1:
{
break;
}
case 2:
{
// new Navigation view
ProductSearchViewController *viewController = [[ProductSearchViewController alloc] initWithNibName:#"ProductSearchViewController" bundle:nil];
viewController.navigationItem.backBarButtonItem.title = #"Back";
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
break;
}
case 3:
{
StoreLocatorNavController *viewController = [[StoreLocatorNavController alloc] initWithNibName:#"StoreLocatorNavController" bundle:nil];
viewController.navigationItem.backBarButtonItem.title = #"Back";
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
break;
}
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
// Dismiss the popover if it's present.
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
// Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
}
NSNotificationCenter part
Add this to ProductSearchViewController:
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[self.productResults objectAtIndex:indexPath.row];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateProduct" object:itemAtIndex];
}
And finally, add this to ProductSearchDetailViewController:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateTheProductDetails:) name:#"updateProduct" object:nil];
}
- (void)updateTheProductDetails:(NSNotification *)notification {
NSDictionary *productDictionary = [NSDictionary dictionaryWithDictionary:[notification object]];
// product name
_productName.text = [productDictionary objectForKey:#"ProductDescription"];
}
Hope it helps!

Resources