故事板准备Seque +传递数据

时间:2012-12-28 16:07:39

标签: objective-c xcode nsmutablearray uilabel uistoryboard

我有一个连接到表格视图单元格的详细视图控制器。我有一个数据模型。它是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;



    }


}

的DetailView

- (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];
}

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

该方法仅被调用detailItem

1 个答案:

答案 0 :(得分: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];