5

I've been following this tutorial to have a Slide-Out Menu. I've added a TableViewController which will display a list of articles. For some reason the viewDidLoad is not firing.

In this tutorial a SideViewController controls which controller will be displayed, in case of having a segue with identifier "showPhoto", it will load a specific image.

// Set the photo if it navigates to the PhotoView
if ([segue.identifier isEqualToString:@"showPhoto"]) {
    PhotoViewController *photoController = (PhotoViewController*)segue.destinationViewController;
    NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.jpg", [menuItems objectAtIndex:indexPath.row]];
    photoController.photoFilename = photoFilename;
}

I thought of recreating the same thing for the TableViewController and trying to force the viewDidLoad of the controller as seen here Force viewDidLoad to fire on iOS but it still didn't work:

if ([segue.identifier isEqualToString:@"showList"]) {
    TableViewController *tableController = (TableViewController*)segue.destinationViewController;
    [tableController view];
}

TableViewController viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Change button color
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f];

    // Set the side bar button action. When it's tapped, it'll show up the sidebar.
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);

    // Set the gesture
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    // Set this view controller object as the delegate and data source for the table view
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;

    // Create array object and assign it to _feedItems variable
    _feedItems = [[NSArray alloc] init];

    // Create new HomeModel object and assign it to _homeModel variable
    _homeModel = [[HomeModel alloc] init];

    // Set this view controller object as the delegate for the home model object
    _homeModel.delegate = self;

    // Call the download items method of the home model object
    [_homeModel downloadItems];
}

What's weird is that the other two Controllers being used (PhotoViewController and MapViewController) work as expected...maybe there's some settings than I'm missing.

I've literally just started on iOS, can't work this out unfortunately.

Project Download

Community
  • 1
  • 1
j.grima
  • 1,831
  • 3
  • 23
  • 45
  • 2
    maybe you are looking for -viewWillAppear: or -viewDidAppear:? – Michael May 31 '14 at 08:43
  • make sure that correct class is being instantiated – Sash Zats May 31 '14 at 08:44
  • @Michael thanks for the suggestion, I've tried moving the `viewDidLoad` code (added to question) to `viewDidAppear` (as suggested by @Goppinath as well), did not change though – j.grima May 31 '14 at 09:17
  • 1
    @j.grima : share this project somewhere. it'll be faster to identify the problem. it's probably a segue related issue. – staticVoidMan May 31 '14 at 09:19
  • @staticVoidMan just added a link to the project, thanks – j.grima May 31 '14 at 09:29
  • 1
    I also tested it. staticVoidMan's answer should fix it. `viewDidLoad` gets called. – Unheilig May 31 '14 at 12:39
  • @Unheilig thank you, I've updated my code and `viewDidLoad` is called...this might be a different issue but just wondering if the TableView displayed any data for you. – j.grima May 31 '14 at 13:57
  • @j.grima Welcome. Unfortunately no, you will soon get an `NSInvalidArgumentException` exception after it gets called. – Unheilig May 31 '14 at 14:19
  • @Unheilig actually I've managed to display the items...`TableViewController.m` had `[self.listTableView reloadData];`, changed that to `[self.tableView reloadData];` and it worked, what's left is having the button working – j.grima May 31 '14 at 14:30
  • @j.grima My focus was rather on the method call, so I didn't delve into the exception. But good that you report back for the benefit of others. – Unheilig May 31 '14 at 14:45
  • @Unheilig no worries! Thanks again for your help. Everything is still quite new to me, I'm more into web and software development through C# but I'm sure I'll get there :) This helped me understand a few things as well. – j.grima May 31 '14 at 14:51
  • I had the same problem and my issue was, I forgot writing class name in Identity Inspector of my Controller in Storyboard. I solved it by just adding class name there. – Mohammad Zaid Pathan Jun 01 '16 at 19:21

2 Answers2

2

Move your code to

- (void)viewDidAppear:(BOOL)animated

hope it will work.

Goppinath
  • 10,569
  • 4
  • 22
  • 45
  • I did as you suggested but still nothing, I will update my question with more code maybe there's something missing – j.grima May 31 '14 at 09:01
2

There are multiple issues with TableViewController (as per the project archive you shared)

TableViewController.h

@interface TableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, HomeModelProtocol>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@end

should be:

//subclass should be UITableViewController
@interface TableViewController : UITableViewController <HomeModelProtocol>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
//not needed
//@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@end

TableViewController.m

in -viewDidLoad, observe:

-(void)viewDidLoad
{
    //...

    //crashes when adding gesture to a tableView (this is not part of the core problem
    //but will be one if not handled) for now... comment it
    //[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    //not needed as it's done via IB (but this is not part of the problem)
    //self.listTableView.delegate = self;
    //self.listTableView.dataSource = self;

    //...
}

Storyboard

Select that UITableViewController:

  • Specify custom class as TableViewController (via Identity Inspector)
  • Set Top Bar to Translucent Navigation Bar (via Attributes Inspector)
  • Add a UINavigationItem
  • Add a UIBarButtonItem on it and set it to menu (as you've done in the other viewControllers)
    • Connect the IBOutlet object sidebarButton to this UIBarButtonItem

SidebarViewController.m

in your -prepareForSegue:sender: method, if you aren't passing data to TableViewController then you need not do anything

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
    //...

    if ([segue.identifier isEqualToString:@"showList"]) {
        //TableViewController *tableController = (TableViewController*)segue.destinationViewController;

        //this is definitely not needed whether you pass data or not
        //[tableController view];
    }

    //...
}
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • I realised the subclass issue just as you posted this :) `viewDidLoad` is now being fired. Just wondering if the table displayed any data when all the code was changed...might be an entirely different issue though. – j.grima May 31 '14 at 13:55
  • Got it to work :) I'm just stuck in the last part of the `UIBarButtonItem` - "Connect the `IBOutlet` object `sidebarButton` to this `UIBarButtonItem`" – j.grima May 31 '14 at 14:43
  • 1
    Figured it out :) I dragged the small circle next to `@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;` in the `TableViewController.h`onto the button in the `Storyboard`, thanks again for you help @staticVoidMan – j.grima May 31 '14 at 15:24