简体   繁体   中英

Can't get one a table view data from another view controller

i'm trying to perform something pretty simple which is:

I have a home page view controller and a table view controller (separate controllers) which supports deleting cells.

In the home page I have a UILabel object which I want to populate with the first cell in the table view controller. So I figured what I need is to create an instance of my table view controller in my home page view controller and ask this information...but from some reason the object i'm getting from the table view controller called "currentTarget" is nil...(and it's not).

This is my HomeViewController: (just the relevant lines)

#import "StackTableViewController.h"

@interface HomeViewController ()

@property (strong, nonatomic) IBOutlet UILabel *homeLabel;

@end

@implementation HomeViewController

- (id)init {
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
        // Do something
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController setNavigationBarHidden:YES];
    self.homeLabel.font = [UIFont fontWithName:@"Candara-Bold" size:40];

    StackTableViewController *svc = [[StackTableViewController alloc] init];
    self.homeLabel.text = svc.currentTarget;
}

This is my StackTableViewController.m: (just the relevant lines)

#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
#import "CoreDataStack.h"

@interface StackTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultController;

@end

@implementation StackTableViewController

- (id)init {

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) {
        // Do something

    }
    return self;
}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    [self.fetchedResultController performFetch:nil];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];

    self.currentTarget = current.body;

}

And also added this code in the prepare for segue method so the currentTarget will get updated coming back from segue:

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

    [self.fetchedResultController performFetch:nil];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];

    self.currentTarget = current.body;
}

why my label won't show up :/?

tnx ahead

The problem is that your init method does not initialise either the fetched results controller or the currentTarget property. They are only initialised When you present that table view controller and the viewDidLoad method runs. If you move (or copy) these lines to your init method, it should work:

[self.fetchedResultController performFetch:nil];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
self.currentTarget = current.body;

In your StackTableViewController.m:

- (id)init {
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
      [self.fetchedResultController performFetch:nil];
      NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
      Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
      self.currentTarget = current.body;    }
    return self;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM