0

에서 하나 개의 테이블 뷰 데이터를 가져올 수 없습니다 세포를 삭제합니다.내가 꽤 간단한 일을 수행하기 위해 노력하고있어 다른 뷰 컨트롤러

홈 페이지에는 테이블보기 컨트롤러의 첫 번째 셀을 채우려는 UILabel 개체가 있습니다. 그래서 내가 필요로하는 것은 내 홈페이지 뷰 컨트롤러에 내 테이블 뷰 컨트롤러의 인스턴스를 생성하고이 정보를 묻는 것입니다.하지만 어떤 이유로 테이블 뷰 컨트롤러에서 "currentTarget"이라는 개체를 얻지 못하고 있습니다. ... (그리고 그렇지 않습니다).

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

이 내 StackTableViewController.m입니다 (다만 관련 라인) : (그냥 관련 라인)

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

} 

그리고 또한 준비에이 코드를 추가

내 HomeViewController입니다 segue 메소드의 경우 currentTarget이 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; 
} 

왜 내 레이블이 나타나지 않습니까? /?

TNX 앞서

+0

를? –

+0

@property (강하고 비 구조) NSString * currentTarget; 그 .h 파일에 재산, 죄송합니다 – JohnBigs

+0

과 target.body도 중첩입니다 – JohnBigs

답변

2

문제는 init 메소드가 가져온 결과 컨트롤러 또는 currentTarget 속성 중 하나를 초기화하지 않습니다. 테이블보기 컨트롤러를 표시하고 viewDidLoad 메서드가 실행될 때만 초기화됩니다. 당신이 이동 (또는 복사)하여 초기화 방법이 줄을 경우 작동합니다 :

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

당신의 StackTableViewController.m에서 : 무엇의`currentTarget` 당신의 StackTableViewController에

- (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; 
} 
관련 문제