TableViewController的viewDidLoad没有触发

时间:2014-05-31 08:39:18

标签: ios objective-c uitableview viewdidload

我一直关注此tutorial以获得幻灯片菜单。我添加了一个TableViewController,它将显示文章列表。由于某种原因,viewDidLoad没有被解雇。

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

// 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,如Force viewDidLoad to fire on iOS所示,但它仍然不起作用:

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

奇怪的是,正在使用的其他两个控制器(PhotoViewControllerMapViewController)按预期工作...可能有一些设置而不是我缺少。

我刚刚开始使用iOS,但遗憾的是无法解决这个问题。

Project Download

2 个答案:

答案 0 :(得分:2)

将代码移至

- (void)viewDidAppear:(BOOL)animated

希望它能奏效。

答案 1 :(得分:2)

TableViewController存在多个问题(根据您共享的项目档案

TableViewController.h

@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 (通过Identity Inspector)
  • Top Bar设为Translucent Navigation Bar (通过属性检查员)
  • 添加UINavigationItem
  • 在其上添加UIBarButtonItem并将其设置为菜单(,就像您在其他viewControllers 中所做的那样)
    • IBOutlet对象sidebarButton与此UIBarButtonItem
    • 相关联

SidebarViewController.m

在您的-prepareForSegue:sender:方法中,如果您没有将数据传递给TableViewController,那么您无需执行任何操作

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

    //...
}