本文介绍了TableViewController的viewDidLoad没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注滑出菜单。我添加了一个TableViewController,它将显示一个文章列表。出于某种原因, viewDidLoad 未触发。

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.

在本教程中, SideViewController 控制将显示哪个控制器,如果有 segue 且标识符为showPhoto,它将加载特定图像。

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

我想为 TableViewController重新创建相同的东西并尝试强制控制器的 viewDidLoad ,如此处所示但它仍然无效:

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

使用其他两个控制器有多奇怪( PhotoViewController MapViewController )按预期工作...可能有一些设置,而不是我错过。

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.

我刚刚开始在iOS上运行,不幸的是无法解决这个问题。

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

推荐答案

那里是 TableViewController 根据您共享的项目档案)的多个问题

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

应为:

//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



-viewDidLoad 中,观察:

-(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;

    //...
}






故事板



选择 UITableViewController


  • 将自定义类指定为 TableViewController

  • 顶栏设置为半透明导航栏

  • 添加 UINavigationItem

  • 添加 UIBarButtonItem 并将其设置为菜单(就像你在其他 viewControllers 中所做的那样)

    • IBOutlet 对象 sidebarButton 连接到此 UIBarButtonItem

    • Specify custom class as TableViewController
    • Set Top Bar to Translucent Navigation Bar
    • 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

      在你的 -prepareForSegue:sender:方法中,如果你没有将数据传递给 TableViewController 然后你不需要做任何事情

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

      这篇关于TableViewController的viewDidLoad没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:04