0

我有一个连接到表格视图单元的详细视图控制器。我有一个数据模型。它是一个NSObject并且拥有NSMutableArray我需要的所有属性。

我将一个标签连接到一个@property并命名它questionLabel。出于某种原因,当我推到详细视图时,它没有显示我在数据模型中为NSMutableArray.

起初我认为 segue 不起作用,但是当我尝试更改导航栏标题时,当我从NSMutableArray.

有人知道我的标签做错了什么吗?

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushDetail"])
    {
        // Create an instance of our DetailViewController
        DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];

        NSIndexPath *path = [self.tableView indexPathForSelectedRow];

        // Setting DQV to the destinationViewController of the seque
        DQV = [segue destinationViewController];

        Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];

        DQV.questionLabel.text = [selectedQuestion questionName];
        DQV.navigationItem.title = [selectedQuestion questionRowName];



    }


}

更新(对于:adambinsz)

表视图

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"pushDetail"])
    {
        // Create an instance of our DetailViewController
        DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];

        // Setting DQV to the destinationViewController of the seque
        DQV = [segue destinationViewController];

        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        Question *selectedQuestion = [self.myQuestionList questionAtIndex:path.row];

        DQV.detailItem = selectedQuestion;



    }


}

详细视图

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        Question *theQuestion = (Question *)self.detailItem;
        self.questionLabel.text = theQuestion.questionName;

        NSLog(@"The Question's questionName is %@", theQuestion.questionName);

     }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

我认为它可能不起作用的原因是因为 if 语句self.detailItem没有正确检查它。

该方法被调用而不是detailItem.

4

1 回答 1

2

当您尝试设置其文本时,您的标签可能尚未创建。我会在创建视图控制器时创建一个selectedQuestion实例变量并设置它。DetailQuestionViewController像这样的东西:

// Create an instance of our DetailViewController
DetailQuestionViewController *DQV = [[DetailQuestionViewController alloc] init];

NSIndexPath *path = [self.tableView indexPathForSelectedRow];

// Setting DQV to the destinationViewController of the seque
DQV = [segue destinationViewController];

DQV.selectedQuestion = [self.myQuestionList questionAtIndex:path.row];

在 DetailQuestionViewController 的viewDidLoad方法中,使用以下方法设置标签的文本:

self.questionLabel.text = [selectedQuestion questionName];
self.navigationItem.title = [selectedQuestion questionRowName];
于 2012-12-28T16:21:23.827 回答