iPhone open multiple view controllers on table view item click - ios

Is it possible to open different view controllers depending on witch table view cell user clicks? I tried to do that with:
[self presentViewController:obj animated:YES completion:nil];
but when next view is presented, there is no navigation bar and I can't go back to table view.
EDIT:
Here is MasterViewController class that I am using
#import "MasterViewController.h"
#interface MasterViewController () {
NSArray *viewArray;
}
#end
#implementation MasterViewController
#synthesize items,itemImges;
- (void)awakeFromNib
{
if ([[[UIDevice currentDevice] systemVersion] compare:#"7" options:NSNumericSearch] != NSOrderedAscending) {
self.preferredContentSize = CGSizeMake(320.0, 480.0);
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
}
self.title = NSLocalizedString(#"MasterTitle",#"Options:");
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
items = [NSArray arrayWithObjects:#"Media Explorer",#"Live TV",#"Settings",nil];
itemImges = [NSArray arrayWithObjects:
[UIImage imageNamed:#"listicon_guide.png"],
[UIImage imageNamed:#"listicon_livetv.png"],
[UIImage imageNamed:#"listicon_settings.png"],
nil];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
MediaExpDetailViewController *DVCA = [self.storyboard instantiateViewControllerWithIdentifier:#"MediaExpDetailViewController"];
LiveTVDetailViewController *DVCB = [self.storyboard instantiateViewControllerWithIdentifier:#"LiveTVDetailViewController"];
SettingsDetailViewController *DVCC = [self.storyboard instantiateViewControllerWithIdentifier:#"SettingsDetailViewController"];
//Create Array of views
viewArray = [NSArray arrayWithObjects:DVCA, DVCB, DVCC, nil];
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = items[indexPath.row];
UIImage *image = itemImges[indexPath.row];
cell.textLabel.text = [object description];
cell.imageView.image = image;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//for iPad
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
//something goes here
}
else { //for iPhone
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
MediaExpDetailViewController *objSynergy = (MediaExpDetailViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"MediaExpDetailViewController"];
[self.navigationController pushViewController:objSynergy animated:YES];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
#end

First set Storyboard IDs for your Next View controllers in Interface Builder and then.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Check Row and Select Next View controller
if (indexPath.row == 1)
{
// Push Selected View
UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryboardID"];
[self.navigationController pushViewController:view1 animated:YES];
}
}

That's because
[self presentViewController:obj animated:YES completion:nil];
Presents the new view controller modally (over top of the existing visible controller). If you want to push to a new view controller using your navigation controller, you'll want to use this. Of course you'll want to make sure that the view controller you're pushing from is embedded within a UINavigationController.
[self.navigationController pushViewController:obj animated:YES];
And to answer your first question, yes it absolutely is possible. Just add some condition logic to your didSelectRowAtIndexPath: UITableViewDelegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (someCondition) {
[self.navigationController pushViewController:obj animated:YES];
}else{
[self.navigationController pushViewController:otherObj animated:YES];
}
}

Modally presented view controllers don't have navigation bars by default. You have to embed them in a UINavigationController, in order to have one. You should also implement how to dismiss the presented view controller by yourself, calling dismissViewControllerAnimated at the appropriate times.
However, I'd recommend to push your view controllers (with pushViewControllerAnimated), instead of presenting them modally, if you don't specifically need the modal functionality.

Related

dismissViewControllerAnimated is not dissmissing ViewController

So....I have a View Controller and when I press a button, another View Controller appears:
- (IBAction)searchButtonPressed:(id)sender {
[self presentViewController:self.controllerSearch animated:YES completion:nil];
}
Inside view controller number 2 is a table view and when a row is selected in a table this code runs:
NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
NSString *filePath2 = filePath; assert(filePath2 != nil); // Path to first PDF file
LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath2 password:phrase];
if (document != nil) // Must have a valid LazyPDFDocument object in order to proceed with things
{
LazyPDFViewController *lazyPDFViewController = [[LazyPDFViewController alloc] initWithLazyPDFDocument:document];
lazyPDFViewController.delegate = self; // Set the LazyPDFViewController delegate to self
#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)
[self.navigationController pushViewController:lazyPDFViewController animated:YES];
#else // present in a modal view controller
lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:lazyPDFViewController animated:YES completion:NULL];
#endif // DEMO_VIEW_CONTROLLER_PUSH
}
else // Log an error so that we know that something went wrong
{
NSLog(#"%s [LazyPDFDocument withDocumentFilePath:'%#' password:'%#'] failed.", __FUNCTION__, filePath2, phrase);
}
Now I am using LazyPDFKit and it comes with this delegate method:
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
// dismiss the modal view controller
[self dismissViewControllerAnimated:YES completion:NULL];
}
I put a break point and I can see my code goes into the delegate method, but the LazyPDFViewController does not go away.
I have tried the following:
[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
but that takes me back a few view controllers to far.
Am I missing something?
Additional code in my first view Controller .h
#property (strong, nonatomic) UISearchController *controllerSearch;
and in first view controller .m
- (UISearchController *)controller {
if (!_controllerSearch) {
// instantiate search results table view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
LHFileBrowserSearch *resultsController = [storyboard instantiateViewControllerWithIdentifier:#"SearchResults"];
// create search controller
_controllerSearch = [[UISearchController alloc]initWithSearchResultsController:resultsController];
_controllerSearch.searchResultsUpdater = self;
// optional: set the search controller delegate
_controllerSearch.delegate = self;
}
return _controllerSearch;
}
If you are pushing the view controller:
[self.navigationController pushViewController:lazyPDFViewController animated:YES];
Then the code in the delegate doesn't make sense, because it assumes it is a modal view controller that needs to be dismissed:
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
// dismiss the modal view controller
[self dismissViewControllerAnimated:YES completion:NULL];
}
But you've added it to the navigation stack (I assume).
If you can't pop it again from the navigation controller at this point you are missing some code in your example.
Are you sure your delegate is firing on the main thread? Try:
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController popViewControllerAnimated:YES];
});
}
try this:
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
// dismiss the modal view controller
[[viewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
your code :
[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
just went too far.
I just made the demo project based on your situation. And I am not facing any issue with it. So I think there might be some issue regarding how you are presenting the second controller.
In your button click, try this code:
- (IBAction)searchButtonPressed:(id)sender {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//idSecondVC is the storyboard id of second view controller
SecondVC *SecondVC = [main instantiateViewControllerWithIdentifier:#"idSecondVC"];
[self presentViewController:SecondVC animated:YES completion:nil];
}
And in your controller number 2, I just used the above code:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"Cell %ld",indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self openLazyPDF];
}
- (void)openLazyPDF
{
NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:#"pdf" inDirectory:nil];
NSString *filePath = [pdfs firstObject]; assert(filePath != nil); // Path to first PDF file
LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath password:phrase];
if (document != nil) // Must have a valid LazyPDFDocument object in order to proceed with things
{
LazyPDFViewController *lazyPDFViewController = [[LazyPDFViewController alloc] initWithLazyPDFDocument:document];
lazyPDFViewController.delegate = self; // Set the LazyPDFViewController delegate to self
#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)
[self.navigationController pushViewController:lazyPDFViewController animated:YES];
#else // present in a modal view controller
lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:lazyPDFViewController animated:YES completion:NULL];
#endif // DEMO_VIEW_CONTROLLER_PUSH
}
else // Log an error so that we know that something went wrong
{
NSLog(#"%s [LazyPDFDocument withDocumentFilePath:'%#' password:'%#'] failed.", __FUNCTION__, filePath, phrase);
}
}
#pragma mark - LazyPDFViewControllerDelegate methods
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
// dismiss the modal view controller
[self dismissViewControllerAnimated:YES completion:NULL];
}
And for me everything is working fine.
Looks like you need to the same macro for present as dismiss. So, you wrote
#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)
[self.navigationController pushViewController:lazyPDFViewController animated:YES];
#else // present in a modal view controller
lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:lazyPDFViewController animated:YES completion:NULL];
#endif // DEMO_VIEW_CONTROLLER_PUSH
You thus need
#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)
[self.navigationController popViewControllerAnimated:YES];
#else // presented in a modal view controller
[self dismissViewControllerAnimated:YES completion:NULL];
#endif // DEMO_VIEW_CONTROLLER_PUSH
It's possible that you have switched off the main thread and you can always add an assert to be check or, as has been suggested, use a dispatch_async to be certain.
NSAssert([NSThread isMainThread)];
I prefer the assert, when I know all the flows through a piece of code, since it shows my assumptions to the future me (or another) and does not leave code that looks like it knows something I do not (oh, they are using dispatch_async onto main so there must be some other thread magic going on deeper down).
- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
if (![NSThread isMainThread])
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self dismissLazyPDFViewController:viewController];
});
return;
}
if (viewController.navigationController)
{
[viewController.navigationController popViewControllerAnimated:YES];
}
else
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}
}

Navigating to different ViewControllers from UITableView

I am new to IOS Objective C Programming. I am trying a very simple app. My main page is standard View Controller where i have a simple UITableView. What i am trying to do is to make a different view controller to come up when user select different cell on the Table View. Currently i populate 3 simple lines. I have searched for many historical discussion but can't seem to work (from segue options to simple instantiate view options). I give a snip of my code and hope that anyone can point me what i am doing wrong here.
Storyboard
Ensure one are you embed with NavigationController or Not, if Not,
just do the following way
XCode menu --> Editor --> EmbedIn --> Navigation Controller, do like
you get the output of
Step-2
In here we can navigate with two types
Connection Through
Connection less
Connection Through
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SegName = #"";
if (indexPath.row == 0)
{
SegName = #"firstSegumeName";
}
else if (indexPath.row == 1)
{
SegName = #"secondSegumeName";
}
else if (indexPath.row == 2)
{
SegName = #"thirdSegumeName";
}
[self performSegueWithIdentifier:SegName sender:self];
}
Connection Less
if use `storyboard ID` with connection less
for example
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *otherViewCon;
if (indexPath.row == 0)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourfirstIdentifierName"];
}
else if (indexPath.row == 1)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yoursecondIdentifierName"];
}
else if (indexPath.row == 2)
{
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:#"yourthirdIdentifierName"];
}
[self.navigationController pushViewController:otherViewCon animated:YES];
}
Create an array with segue names in the order in which you want to refer to the view controllers from your tableview and then in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
you can call
[self performSegueWithIdentifier:[array valueAtIndex:indexPath.row sender:nil];
If its through XIB :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:#"Your_ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
If its through StoryBoard :
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_storyboard" bundle:[NSBundle mainBundle]];
Your_ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"storyboardID"];
[[self navigationController] pushViewController:vc animated:YES];
If its through Coding :
Your_ViewController *vc = [[Your_ViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
For Your questions :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Put above code here according to your requirement....
}
Try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
firstViewController *u = [[ContactDetailsVC alloc]init];
u= [self.storyboard instantiateViewControllerWithIdentifier:#"firstView"];
[self.navigationController pushViewController:u animated:YES];
}
Replace last 2 lines of didSelectRowAtIndexPath with:
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:myView];
[self presentViewController:navC animated:YES completion:nil];

setDetailItem:(id)newDetailItem not firing

I am updating my iPad app and I've got a list of options in the table of the split view controller, but it's not firing the setDetalItem is not firing. This will not be ported to the iPhone because of the form-factor (screen is too small).
From the appDelegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
JSLMasterViewController *controller = (JSLMasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
from the masterViewController:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
JSLDetailViewController *detailView = self.detailViewController;
detailView.telegram = indexPath.row;
NSLog(#"Did Fire 1");
}
And from my detailViewController:
- (void)setDetailItem:(id)newDetailItem
{
NSLog(#"Did Fire 2");
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
//self.detailDescriptionLabel.text = [[self.detailItem valueForKey:#"timeStamp"] description];
NSArray *mainQuestion = [self mainQuestionArray];
NSArray *subQuestion = [self subQuestionArray];
questionTitle.text = mainQuestion[telegram];
subQuestionOne.text = subQuestion[telegram][0];
subQuestionTwo.text = subQuestion[telegram][1];
}
}
I understand that I may need to create an instance of the detailViewController in the appDelegate, but I am unsure as to how to do this. Most of the tutorials I have found build a split view from scratch or seem to be using an older version of the mechanism. Any tips you can give me would be greatly appreciated!
You need to set detailView.detailItem in order for the - (void)setDetailItem:(id)newDetailItem to fire.
In the MasterViewController.m detailViewController is usually something like:
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
and then in the didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSYourCoolObject *object = _YourObjectArray[indexPath.row];
self.detailViewController.detailItem = object;
}
Turns out that, in iOS6, one needs to call, "setDetailItem" directly. You can pass objects here, although it's not necessarily what you would think. I added the following code to didSelectItemAtIndexPath:
[detailView setDetailItem:indexPath];
This fired the update to the MasterViewController and life is good.

UIViewController may not respond to showRootPopoverButtonItem

I am using Apple's MuiltipleDetailViewController sample app and get the message "UIViewController may not respond to showRootPopoverButtonItem"
This worked in XCode 3.X, but I get the message with 4.2
The app itself functions 100%, the popover is recognized in every nib, as is the table on the left when in landscape mode. But I can't submit with this warning. What do I need to change??
RootViewController.h
#import <UIKit/UIKit.h>
/*
SubstitutableDetailViewController defines the protocol that detail view controllers must adopt. The protocol specifies methods to hide and show the bar button item controlling the popover.
*/
#protocol SubstitutableDetailViewController <NSObject>
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
#end
#interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {
UISplitViewController *splitViewController;
UIPopoverController *popoverController;
UIBarButtonItem *rootPopoverButtonItem;
//UINavigationBar *navigationBar;
}
#property (nonatomic, assign) IBOutlet UISplitViewController *splitViewController;
#property (nonatomic, retain) UIPopoverController *popoverController;
#property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;
//#property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;
#end
RootViewController.m:
#import "RootViewController.h"
#import "WebViewController.h"
#import "Twitter.h"
//#import "SubstitutableDetailViewController.h"
#implementation RootViewController
#synthesize splitViewController, popoverController, rootPopoverButtonItem;//, navigationBar;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Set the content size for the popover: there are just two rows in the table view, so set to rowHeight*2.
self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*2.0);
//self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255/255 green:104/255 blue:1/255 alpha:1];
}
/*
-(void)customizeAppearance {
//create resizable images
UIImage *bluImage = [UIImage imageNamed:#"blu.jpg"];// resizableImageWithCapInsets:(0, 0, 0, 0)];
//set the bg for *all* UINavBars
[[UINavigationBar appearance] setBackgroundImage:bluImage forBarMetrics:UIBarMetricsDefault];
}
*/
-(void) viewDidUnload {
[super viewDidUnload];
self.splitViewController = nil;
self.rootPopoverButtonItem = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc {
// Keep references to the popover controller and the popover button, and tell the detail view controller to show the button.
barButtonItem.title = #"Index";
self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;
UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController showRootPopoverButtonItem:rootPopoverButtonItem];
}
- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
// Nil out references to the popover controller and the popover button, and tell the detail view controller to hide the button.
UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
[detailViewController invalidateRootPopoverButtonItem:rootPopoverButtonItem];
self.popoverController = nil;
self.rootPopoverButtonItem = nil;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Two sections, one for each detail view controller.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"RootViewControllerCellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Set appropriate labels for the cells.
if (indexPath.row == 0) {
cell.textLabel.text = #"Twitter";
}
else if (indexPath.row == 1) {
cell.textLabel.text = #"Contact Us";
}
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blackColor];
cell.contentView.backgroundColor = [UIColor blackColor];
cell.detailTextLabel.backgroundColor = [UIColor blackColor];
return cell;
}
#pragma mark -
#pragma mark Table view selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController *detailViewController = nil;
if (row == 0) {
Twitter *newDetailViewController = [[Twitter alloc]
initWithNibName:#"Twitter"
bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
WebViewController *newDetailViewController = [[WebViewController alloc]
initWithNibName:#"WebViewController"
bundle:nil];
newDetailViewController.detailURL=
[[NSURL alloc] initWithString:#"http://www.chipmunkmobile.com/contact.html"];
detailViewController = newDetailViewController;
}
// 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];
}
#pragma mark -
#pragma mark Managing the popover
/*
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Add the popover button to the left navigation item.
[navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Remove the popover button.
[navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
}
*/
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[popoverController release];
[rootPopoverButtonItem release];
[super dealloc];
}
#end
Here is an image of the exact line where I get the warning
The reason you getting this warning is because of this UIViewController *detailViewController UIViewController does not have a method called "showRootPopoverButtonItem". If you want to get rid of the warning just do this instead:
[(WebViewController*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
or
[(Twitter*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
You just need to let it know its not really just a viewController, its a subclassed viewController you created. So what ever class showRootPopoverButtonItem: is in you just need to type cast it.
If you want to leave your code exactly the same but get rid of the warning you can do this.
if (rootPopoverButtonItem != nil) {
[detailViewController performSelector:#selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
}
If you want to be more careful you should use this.
if (rootPopoverButtonItem != nil && [detailViewController respondsToSelector:#selector(showRootPopoverButtonItem:)]) {
[detailViewController performSelector:#selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
}
Update
After reading through your code you could just specify the delegate on the detailViewController like you did in the other functions.
UIViewController<SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
//...
(1)Specify the Type for the detailedViewController: [(TYPE *)OBJECTNAME...
(2) Then Specify method call [(TYPE *)OBJECTNAME METHODCALL];
(*) You get the warning because the compiler does not know what type of object you are using. If you subclass a UIViewController then you have to specify type when accessing methods.Make sure the method is in .h so that you can access it.

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