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
There are multiple issues with TableViewController
(as per the project archive you shared)
@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
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;
//...
}
Select that UITableViewController
:
TableViewController
(via Identity Inspector)
Top Bar
to Translucent Navigation Bar
(via Attributes Inspector)
UINavigationItem
UIBarButtonItem
on it and set it to menu (as you've done in the other viewControllers
)
IBOutlet
object sidebarButton
to this UIBarButtonItem
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];
}
//...
}
Move your code to
- (void)viewDidAppear:(BOOL)animated
hope it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With