簡體   English   中英

無法將視圖控制器屬性獲取到另一個視圖控制器

[英]can't get a view controller property to another view controller

我有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出了點問題,因為它沒有填充標簽。

我該如何解決?

n

您可能應該在StackTableViewController的ViewWillAppear中更新標簽的文本

另外,是否在StackTableViewController的init方法中設置了currentTarget? 如果沒有,它將不會被初始化,這就是為什么您在HomeViewController的標簽中沒有得到任何東西的原因

說明

不會在viewDidLoadinit之前調用viewDidLoad 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方法,因此將調用超類的initUIViewController )。 當您實例化ViewController它將開始加載視圖。 只有在完成加載后,才會調用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];

}

主視圖控制器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM