Register  |  Login

 Blog List
Minimize
 RSS
Minimize
You must be logged in and have permission to create or edit a blog.
 View Blog
Minimize
Oct 6

Written by: Usman Shabbir
10/6/2010 4:44 PM 

You might find lots of Blogs and Books to use Split view controller in a normal way with one root view controller and a detail view controller like a really nice tutorial can be seen here. You might also find a tutorial that will tell you how to call different detail view controllers from root view controller here.

But when it comes to call detail view controller from a controller other then Root view controller help just diminish. As we can use navigation controller in root view controller that will navigate to a controller which is not defined in app delegate for split view controller then comes the real deal of calling it from last controller in navigation.

The solution that i proposed here is to use NSnotifications to broad cast a message with proper name and then use that message in detail view controller to call a function in detail view controller and for the detail item that is to be passed use the Object: parameter on NSnotification to send an object to it which can be utilized in detailview controller.

Lets check some code snippits for better elaboration.

Here is a root view controller didSelectRowatindexPath function from which mostly detail view controller is called but we need some navigation so i am calling a TestViewController by using navigation of root view controller.

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = [indexPath row];

Category *category = [categories objectAtIndex:index];


TestViewController *shopList = [[TestViewController alloc] initWithNibName:@"TestViewController " bundle:[NSBundle mainBundle]];
shopList.categoryId = category.categoryId;
[[self navigationController] pushViewController:shopList animated:YES];
[shopList release];

}



Now to Reach to a test view controller from which a detail view controller needs to be called in the didSelectRowatindexPath function of this viewcontroller we will be broadcasting an NSTotification

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = [indexPath row];

[[NSNotificationCenter defaultCenter] postNotificationName:@"dealNotification" object:[article;

}


That all for a new controller in navigation controller. now we move to detail view controller where in ViewdidLoad an observer is added and the function that needs to be called is passed to selector.

- (void)viewDidLoad {

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(iamcallingyou:) name:@"dealNotification" object: nil];
[super viewDidLoad];
}


Now finally the function whose name is given in selector.

- (void) iamcallingyou:(NSNotification *)pNotification
{

NSLog(@"hi");
// This is a Class Object that i have passed in to object parameter when broadcasting notifications.
article = (Article *)[pNotification object];

if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}


}



And we are done with it... do some memory management in final touches and we are good to go

Happy Coding.

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel