views:

137

answers:

1

Hi all,

So we have recently started developing applications for the iPad for our company. Unfortunately none of us here have ever done any iPhone/iPad development so we are just kind of learning on the fly and winging it. Basically our problem is happening when we try to push a view onto the screen when a table row is clicked. Neither of us can figure out why it's not doing it because the code looks 100% correct by anything we can tell. Here is what the didRowSelectedAtIndex: method looks like:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

 if(pdvController == nil)
  pdvController = [[PersonDetailViewController alloc] initWithNibName:@"PersonDetailView" bundle:[NSBundle mainBundle]];

 Person *aPerson = [appDelegate.people objectAtIndex:indexPath.row];

 pdvController.aPerson = aPerson;

 [self.appDelegate.navigationController pushViewController:pdvController animated:YES];
}

and here is the header for the toolbarSearchViewController:

  #import <UIKit/UIKit.h>

    #import "RecentSearchesController.h"
    #import "PersonDetailViewController.h"

    @class ToolbarSearchAppDelegate, PersonDetailViewController;

    @interface ToolbarSearchViewController : UIViewController <UISearchBarDelegate, UIPopoverControllerDelegate, RecentSearchesDelegate> {

     ToolbarSearchAppDelegate *appDelegate;

        UIToolbar *toolbar;
        UISearchBar *searchBar;
     UITableView *resultsTable;

        PersonDetailViewController *pdvController;
        RecentSearchesController *recentSearchesController;

     UITableViewController *resultsTableController;
        UIPopoverController *recentSearchesPopoverController; 

    }


    @property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
    @property (nonatomic, retain) UISearchBar *searchBar;
    @property (nonatomic, retain) IBOutlet UITableView *resultsTable;

    @property (nonatomic, retain) ToolbarSearchAppDelegate *appDelegate;
    @property (nonatomic, retain) PersonDetailViewController *pdvController;
    @property (nonatomic, retain) UITableViewController *resultsTableController;
    @property (nonatomic, retain) RecentSearchesController *recentSearchesController;
    @property (nonatomic, retain) UIPopoverController *recentSearchesPopoverController;



    @end

and the ToolbarSearchAppDelegate header:

#import <UIKit/UIKit.h>

@class ToolbarSearchViewController;

@interface ToolbarSearchAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ToolbarSearchViewController *viewController;
 UINavigationController *navigationController;


 NSMutableArray *people;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ToolbarSearchViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) NSMutableArray *people;

@end

and finally the personDetailController.h:

#import <UIKit/UIKit.h>

@class Person;

@interface PersonDetailViewController : UIViewController {

 IBOutlet UITableView *tableView;

 Person *aPerson;
}

@property (nonatomic, retain) Person *aPerson;
@property (nonatomic, retain) UITableView *tableView;

@end

We are getting pretty annoyed because neither of us can figure out why it's not loading and any help or insight you guys could provide would be amazing. I wasn't sure what you may need to see, so if you need anything more specific, let me know and I will post it. Thanks!

ps. I am not sure how the code tags are going to handle all of Obj-c's quirky syntax, so if you see syntax errors, just ignore them. Assume that it is syntactically correct and will compile and run...

+1  A: 

This line looks a little odd:

[self.appDelegate.navigationController pushViewController:pdvController animated:YES];

try and make it

[self.navigationController pushViewController:pdvController animated:YES];

edit summing op, the problem is that you don't have a navigation controller, so you can't push a view controller like that. What you can do, is pushing a view controller modally, using

[self presentModalViewController:pdvController animated:YES];
mvds
Yeah, that line is what we initially had. I forgot we changed it to the other line in an attempt to try something different. Changing it back to that line did not make any difference. I am going to try putting the print statements all over right now to see if I can figure out what is going on...
gabaum10
In another note, I have stepped through it a million times in the debugger and it actually gets to those lines and executes them all, it just doesn't do anything. It makes me think that the navigation controller is not hooked up to the right outlets properly, but I just am not sure where it should be.
gabaum10
if you never set `self.appDelegate` manually, it is nil and the debugger happily "executes" `[nil.navigationController ...]` which is a NOOP. `NSLog(@"navcnt: %x",self.navigationController);` to see where you are sending your message to.
mvds
Hmm that `didSelectRowAtIndexPath`, is that in your view controller? If not, put it there, so self.navigationController points to the right object by definition -- that is, if there is a navigationController to start with; do you have one in IB?
mvds
Ok, I have this line in the viewDidLoad: method:appDelegate = (ToolbarSearchAppDelegate *)[[UIApplication sharedApplication] delegate]; that should do the trick setting the appDelegate correct? Also, the print statement above returns:2010-08-05 10:43:55.787 ToolbarSearch[727:207] navcnt: 0I assume that is acceptable? Or do you want some sort of response there?
gabaum10
Yes, that is in the ToolbarSearchViewController.As for the navigation controller in IB, there is one, but I am not sure if it is connected properly. Should it exist in the main window, or the current view? The whole object model diagram that this uses is strange to me still.
gabaum10
CHECK there's your problem, self.navigationController is `nil`, i.e. not set. So you're not pushing the view controller anywhere. See my previous comment on the navigationController.
mvds
by current view I mean the one where I am trying to push the next view...
gabaum10
this is getting a little too complicated to fix through the comments, if you put up a link to a zip I'll have a look.
mvds
ok, that's what I suspected...
gabaum10
Ok here is the link to the project. Obviously I removed the search strings for security reasons, but everything else should be there.http://gabaum06.fileave.com/ToolbarSearch%20with%20no%20search%20string%202.zip
gabaum10
Ok, this is the issue: You don't have a navigation controller. The easiest fix is to present the details *modally*, using `[self presentModalViewController:pdvController animated:YES];`. Otherwise you should bring back the navigationcontroller in the mix - but I fear that would be a *little* beyond the Stackoverflow "answering questions for rep points" exchange. If you want me to have a further look though, let me know.
mvds
It worked perfectly. Thanks so much for your help. I want to learn how the navigation controller should work properly, but I can do that on my own.
gabaum10