iPad: SplitViewController with navigation - cocoa-touch

I'm following Apple sample code "MultipleDetailViews" but what I want to do is that:
at start, shows the RootViewController (table view) display the default detailViewController (1st detailView)
when user selected a table cell, push into the stack, display the SubCategoriesVC (table view) in the master of the splitView but don't update the detailView.
in SubCategoriesVC, selecting a table cell.. update the detailViewController (2nd detailView)
So, in RootViewController.m, I push another navigation
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SubCatVC *browseSubCatView = [[SubCatVC alloc] initWithNibName:#"SubCatVC" bundle:nil];
[self.navigationController pushViewController:browseSubCatView animated:YES];
[browseSubCatView release];
}
Then, in SubCatVC.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailViewController" bundle:nil];
detailViewController = newDetailViewController;
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
self.splitViewController.viewControllers = viewControllers;
[viewControllers release];
// Dismiss the popover if it's present.
if (self.popoverController != nil) {
[self.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 (self.rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateProduct" object:nil];
}
but it didn't update my detailView.. so I don't know what's wrong?
Src here: http://pastebin.com/iy6SqLqt
Hope someone can advise me. Thanks

I Have not looked at your source, but but a common problem with Split views are delegates.
When you Push a new rootController ont the nav stack, you need to make sure it has the pointer to the detailViewController that you want it to talk to. You can check htis my logging the delegate before you try to update it:
In your SubCatVC (or any root for that matter):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//do stuff, init alloc vc's....
NSLog(#"DELEGATE: %#",self.delegate);
//push/delegate etc..
}
If you find its nil, copy the original roots delegate, or when your putting your new detail in, notify/set the root you want to comunicate with it.
If you need any more detail, just ask.

Related

Push a string and display in a UITextField

I am doing a small app and I am using a UISplitVIewController and I am trying to push the UITable cell text to the detail view and display it in a UITextField. I have managed to push the string nicely when i test it with a NSLog but when i apply to the UITextField it does not display not sure why here my method: (I am doing this using a storyboard)
-(void)pushModuleName:(NSString*)moduleName
{
self.Lv4ModuleTitleTextField.text = moduleName;
NSLog(#"name pushed%#",moduleName);
}
Not sure why this doesn't work.
[UPDATE]
UITableViewController.m (where the method is called)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedModule = [NSString stringWithFormat:#"%#",[_numberOfRows objectAtIndex:indexPath.row]];
Lv4GradeViewController *lv4 = [[Lv4GradeViewController alloc] initWithNibName:nil bundle:nil];
[lv4 pushModuleName:selectedModule];
}
You are creating a new instance of Lv4GradeViewController with the following code but no XIB file which will contain the text field:
Lv4GradeViewController *lv4 = [[Lv4GradeViewController alloc] initWithNibName:nil bundle:nil];
I would suggest you instantiate Lv4GradeViewController with the storyboard instantiateViewControllerWithIdentifier method:
Lv4GradeViewController *lv4 = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
And remember to set the storyboard ID of the Lv4GradeViewController the same as IdentifierName in the storyboard.
The problem is that you're not getting a reference to the Lv4GradeViewController that you have on screen, you're creating a new one with alloc init. A split view controller, has a viewControllers array, with the controller at index 0 being the master controller, and the one at index 1 being the detail controller. So, the didSelectRowAtIndexPath method should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedModule = [NSString stringWithFormat:#"%#",[_numberOfRows objectAtIndex:indexPath.row]];
Lv4GradeViewController *lv4 = self.splitViewController.viewControllers[1];
[lv4 pushModuleName:selectedModule];
}

Basic iOS issues

I am trying to to complete the 'Your Second iOS App' tutorial from the apple developer program. It is a basic tableView app. My problem is that the app is building successfully and without warnings, however I cannot get the detail view to segue from the master view. I have copied and pasted both the segue identifier and the code that apple has provided. The segue is using push and I have deleted it and tried again several times. I am testing the app in the simulator.
How can I tell if a segue is working?
Every time I copy/paste my code from Xcode into the stack overflow question text area, I get warnings at the bottom saying that code must be indented by 4 spaces??? Does this mean I have to go line-by-line indenting code?? I did the control + k and pasted in the highlighted area, however I still got the warning??
When running the simulator and looking at it, I'm trying to use a disclosure indicator by clicking on it, do I have to push something special like control = click or command = click, etc. ?
Here is the code for BirdsMasterViewController.m file:
//
// BirdsMasterViewController.m
// BirdWatching
//
// Created by David Hall on 11/13/12.
// Copyright (c) 2012 David Hall. All rights reserved.
//
#import "BirdsMasterViewController.h"
#import "BirdsDetailViewController.h"
#import "BirdSightingDataController.h"
#import "BirdSighting.h"
/*
#interface BirdsMasterViewController () {
NSMutableArray *_objects;
}
#end
*/
#implementation BirdsMasterViewController
- (void)awakeFromNib
{
[super awakeFromNib];
self.dataController = [[BirdSightingDataController alloc] init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
*/
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
/*- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
*/
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"BirdSightingCell";
static NSDateFormatter *formatter = nil;
if (formatter == nil)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:sightingAtIndex.name];
[[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
/*- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowSightingDetails"]) {
BirdsDetailViewController *detailViewController = [segue destinationViewController];
detailViewController.sighting = [self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
#end
And here is the code for the BirdsDetailViewController.m
//
// BirdSightingDataController.m
// BirdWatching
//
// Created by David Hall on 11/25/12.
// Copyright (c) 2012 David Hall. All rights reserved.
//
#import "BirdSightingDataController.h"
#import "BirdSighting.h"
#interface BirdSightingDataController ()
- (void)initializeDefaultDataList;
#end
#implementation BirdSightingDataController
- (void)initializeDefaultDataList
{
NSMutableArray *sightingList = [[NSMutableArray alloc] init];
self.masterBirdSightingList = sightingList;
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithName:#"Pigeon" location:#"Everywhere" date:today];
[self addBirdSightingWithSighting:sighting];
}
- (void)setMasterBirdSightingList:(NSMutableArray *)newList
{
if (_masterBirdSightingList != newList)
{
_masterBirdSightingList = [newList mutableCopy];
}
}
- (id)init
{
if (self = [super init])
{
[self initializeDefaultDataList];
return self;
}
return nil;
}
- (NSUInteger)countOfList
{
return [self.masterBirdSightingList count];
}
- (BirdSighting *)objectInListAtIndex:(NSUInteger)theIndex
{
return [self.masterBirdSightingList objectAtIndex:theIndex];
}
- (void)addBirdSightingWithSighting:(BirdSighting *)sighting
{
[self.masterBirdSightingList addObject:sighting];
}
#end
David Hall
It's working when it works. But without knowing more about what you are doing it's hard to give you an answer.
Highlight the code in the textbox and then click on this button.
It will indent the code correctly for you.
I can't make sense of this question.
Edited to add
(After seeing a copy of the project that the OP mailed me)
Your segue wasn't wired up properly.
The segue is supposed to go from the cell to the next view controller, because the point is to transition on a cell click. Your segue is wired up from the controller to the detail view controller. In your project - right click on your view controller and you'll see the segue is manually connected. But right clicking on the cell doesn't show a segue connection.
Delete the current segue and recreate it, this time by control-dragging from the cell to the next view controller. You can then double check the connection by right clicking on the cell and making sure that the segue is connected.
It should look like:
If the segue is supposed to transition from your table to a detail view, and if you can't get to the detail view from the table, then your segue isn't working.
You can use the code sample button at the top of the SO editor, or you can select the code in your text editor and indent it before you copy it. For example, in Xcode it's easy to select the code, hit Command-], and copy. Then just paste into the SO editor.
Please edit your question so that it makes sense. However, I think you might be asking something along the lines of how to push a view controller from your table. If you're using a segue, and if you've created the segue in your storyboard and it's connected to both the source and destination view controllers, then you can send -performSegueWithIdentifier:sender: to your table's view controller. That is, your table view controller's -tableView:didSelectRowAtIndexPath: method should call -performSegueWithIdentifier:sender: and specify the identifier for the segue leading from the table view controller to the detail view controller. It doesn't matter whether you're using the simulator or running your app on a real device.
See the other answers from Caleb and Abizem regarding 1. and 2. If I understood your question No 3 correctly, then the answer is no. You do not have to press something in particular in the simulator when you want to select a table row or the detail desclosure indicator. Just click on those items that you would tap on your device too. If it does not segue then the chances are that its not the simulator causing the problem :)

tableView: didSelectRowAtIndexPath: inconsistency

I'm fairly new to Objective-C and iOS app development so I apologize if the answer to my question is simple. I'm trying to send a pointer of a GTGift object from one view controller to another. When I enter the following code, the object is send and stored in an instance variable of the second view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
GTGift *selectedGift = [[[GTGiftStore sharedStore] allGifts] objectAtIndex:[indexPath row]];
GTDetailViewController *dvc = [[self storyboard] instantiateViewControllerWithIdentifier:#"detailVC"];
[dvc setDetailGift:selectedGift];
[[self navigationController] pushViewController:dvc animated:YES];
}
However, when I enter this code in the first view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
gift = [[[GTGiftStore sharedStore] allGifts] objectAtIndex:[indexPath row]];
GTDetailViewController *dvc = [[self storyboard] instantiateViewControllerWithIdentifier:#"detailVC"];
[[self navigationController] pushViewController:dvc animated:YES];
}
And this code in the second view controller:
- (void)viewWillAppear:(BOOL)animated {
GTGiftsViewController *gvc = [[self storyboard] instantiateViewControllerWithIdentifier:#"giftsVC"];
detailGift = [gvc gift];
NSLog(#"%#", detailGift);
}
detailGift returns null.
I can't for the life of me understand why, and it would be much more practical for my application if I could use an approach similar to the code segment that does not work.
If anyone can shine some light on this topic for me or point me in a different direction to complete the same task I would really appreciate it!
Your problem is that the word "instantiate" means what it says. :-)
In viewWillAppear you're creating a brand new GTGiftsViewController object which knows nothing about the gift that's been selected in the first GTGiftsViewController object.
(The "storyboard way" of doing this kind of thing is to create a prepareForSegue:sender: method in your first view controller and do the work there.)

UITableViewController Grouped Property Not Working

I have two UITableViewControllers. In the first UITableView, once a user selects a cell, a new UITableViewController is pushed. I have set both UITableViews to "Grouped" in IB. However, when the second UITableViewController is pushed, it appears as a "Plain" UITableView. Is there a way to fix this?
Just as a sanity check, I changed the code so that the second UITableViewController is pushed not from the first UITableViewController, and it does appear to be "Grouped". Is there a reason this is happening?
Code From UITableViewController that is pushing second UITableViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.text isEqualToString:#"Long Term Disability"]) {
LongDisabilityTableView *ldvc = [LongDisabilityTableView alloc];
[self.navigationController pushViewController:ldvc animated:YES];
}
if ([cell.textLabel.text isEqualToString:#"Short Term Disability"]) {
ShortDisabilityTableView *sdvc = [ShortDisabilityTableView alloc];
[self.navigationController pushViewController:sdvc animated:YES];
}
}
If you are pushing to a UITableViewController, you can force the table to be grouped by doing one of the following:
Init
MyTableController *grouped = [[MyTableController alloc] initWithStyle:UITableViewStyleGrouped];
Super
This needs to be added to a UITableViewController.
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped]; /* This is where the magic happens */
if (self) {
self.title = #"My Grouped Table";
}
return self;
}
Double Check
Make sure that you are not placing your UITableView on a UIViewController.
Make sure that you are calling the correct controller in your code (from didSelectRowAtIndexPath:)
Update, added after code
Well there's one reason right there, you aren't using init. See my first example above.
You should also change your code to this:
/* This assumes that your Long Term Disability cell is at index 0
and that your Short Term Disability cell is at index 1.
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ( indexPath.row )
{
case 0: /* #"Long Term Disability" */
LongDisabilityTableView *ldvc = [[LongDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:ldvc animated:YES];
break;
case 1: /* #"Short Term Disability" */
ShortDisabilityTableView *sdvc = [[ShortDisabilityTableView alloc] initWithStyle:UITableViewStyleGrouped];
[self.navigationController pushViewController:sdvc animated:YES];
break;
}
}

Accessing UITableViewController > UIView > UIView?

Can anyone tell me if this is possible?
I have a setup a UITableViewController which consists of a number of cells, if you select one of these cells you are taken to a new viewController using pushViewController (see below)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSLog(#"CELL__: You tapped the row ... %#", [list objectAtIndex:row]);
TestController *testController = [[TestController alloc] init];
[[self navigationController] pushViewController:testController animated:YES];
[testController release];
}
If in the implementation for "TestController" I would like to refer back to [self navigationController] (or maybe to add yet another view) is there any way to do that. My problem is that I need to access [self navigationController] to push further views and I don't know how to do that from the bottom controller?
Does that make sense?
Gary
From within TestController you reference the same UINavigationController via [self navigationController] as well.
EDIT to elaborate after comment:
The navigationController property is a property of UIViewController. If the controller is contained within a UINavigationController then it's own navigationController property will be a reference to that container UINavigationController. The same applies for tabBarController and splitViewController.