【发布时间】: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