无法将视图控制器属性获取到另一个视图控制器

时间:2014-12-09 23:09:52

标签: ios objective-c iphone

我有2个视图控制器:

StackTableViewController.m:

@interface StackTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultController;

@end

@implementation StackTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

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

HomeViewController.m

#import "HomeViewController.h"
#import "CreateViewController.h"
#import "StackTableViewController.h"

@interface HomeViewController ()

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

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    StackTableViewController *vc = [[StackTableViewController alloc] init];
    NSString *current = vc.currentTarget;
    // Do any additional setup after loading the view.
    self.targetLabel.text = current;
}

但是HomeViewController有问题,因为它没有填充标签..

我该如何解决?

TNX

2 个答案:

答案 0 :(得分:0)

您应该更新StackTableViewController的ViewWillAppear中的标签文本

另外,在StackTableViewController的init方法中是否设置了currentTarget?如果没有,它将无法初始化,这就是为什么你在HomeViewController的标签中没有得到任何东西

答案 1 :(得分:0)

解释

不会调用

viewDidLoad,或者在init之前调用init!在- (void)viewDidLoad { [super viewDidLoad]; StackTableViewController *vc = [[StackTableViewController alloc] init]; // init is called on vc object NSString *current = vc.currentTarget; // ViewDidLoad was not yet called on vc object! self.targetLabel.text = current; } /// ViewDidLoad will be called on vc object sometime after leaving this method! 方法完成之后,有时会将其称为

会发生什么:

StackTableViewController *vc = [[StackTableViewController alloc] init];

会发生什么: 您致电init,向对象发送消息init 您的实施中没有init方法,因此调用了超级类UIViewControllerViewController)。当您实例化ViewDidLoad时,它开始加载视图。只有在完成加载后才会调用ViewController

请阅读@implementation StackTableViewController { id<TargetChangedDelegate> _myDelegate; } -(id)initWithDelegate:(id<TargetChangedDelegate>)delegate { _myDelegate= delegate; } - (void)viewDidLoad { [super viewDidLoad]; [self.fetchedResultController performFetch:nil]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; Target *record = [self.fetchedResultController objectAtIndexPath:indexPath]; self.currentTarget = record.body; // inform your delegate [_myDelegate targetChanged]; } lifecycle上的任何文章。

解决方案

解决问题的最快方法:在表格中创建一个委托,并在使用委托加载完成后告知,如下所示:

表视图控制器

@interface HomeViewController () <TargetChangedDelegate>

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

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    StackTableViewController *vc = [[StackTableViewController alloc] initWithDelegate:self];
}

- (void)targetChanged
{
    NSString *current = vc.currentTarget;
    // Do any additional setup after loading the view.
   self.targetLabel.text = current;
}

主视图控制器

{{1}}