TableViewController's viewDidLoad not firing

2019-04-09 00:25发布

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

2条回答
混吃等死
2楼-- · 2019-04-09 00:47

Move your code to

- (void)viewDidAppear:(BOOL)animated

hope it will work.

查看更多
冷血范
3楼-- · 2019-04-09 00:52

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];
    }

    //...
}
查看更多
登录 后发表回答