【问题标题】:Storyboard Preparing for Seque+Passing over data故事板准备序列+传递数据
【发布时间】:2012-12-28 16:07:39
【问题描述】:

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

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

该方法被调用,而不是detailItem

【问题讨论】:

  • detailItem的自定义setter方法需要什么?为什么不在 DetailQuestionViewController 上创建一个 Question 变量,然后将其设置在 prepareForSegue 中?这应该很简单,我认为你过于复杂了。
  • 你能给我举个例子吗?谢谢。
  • 参考我原来的答案。

标签: objective-c xcode nsmutablearray uilabel uistoryboard


【解决方案1】:

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

// 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];

【讨论】:

  • 没有。它仍然对我不起作用。现在,连navigationItem 标题都没有出现。
  • 您可以发布您的代码现在的样子吗?另外,请确保 [selectedQuestion questionName] 返回一个有效的 NSString。
  • 我想我可能做错了实例变量。你要我这样宣布吗? '@property (strong, nonatomic) id selectedQuestion;'
  • 应该是@property (nonatomic, strong) Question *selectedQuestion;,你还需要在DetailQuestionViewController.h的顶部添加#import "Question.h"(或者任何你的问题类文件的名称)。
  • 抱歉,还是不行。我把它改成你说的,但结果还是一样。
猜你喜欢
  • 2012-01-15
  • 2011-12-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2013-05-10
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多