I have a TableView setup as my "root view controller" and a detail view controller called "DetailController" to allow me to display the detail view for what the user selects in the main table view. The navigation is working correctly - when I select a row in my table the screen slides to the left and I'm presented with my detail view, but I can't update the detail view's UILabel text properties. It's like it is just ignoring my UILable set property methods (see code below) as it isn't giving me any errors or warnings.
我有一个TableView设置作为我的“根视图控制器”和一个名为“DetailController”的详细视图控制器,允许我显示用户在主表视图中选择的内容的详细视图。导航工作正常 - 当我在表格中选择一行时,屏幕向左滑动,我会看到我的详细视图,但我无法更新详细视图的UILabel文本属性。这就像它忽略了我的UILable set属性方法(见下面的代码),因为它没有给我任何错误或警告。
I've tested that I do have the values I need when I hit the "tableView:didSelectRowAtIndexPath:" method, but for some reason I can't seem to update the detail view's UILabels' text properties. Using NSLog's I can see that I am getting the values and passing them correctly to the UILabels. However the detail view isn't showing the passed values in its UILabels.
我已经测试过,当我点击“tableView:didSelectRowAtIndexPath:”方法时,我确实拥有了我需要的值,但由于某种原因,我似乎无法更新详细视图的UILabels文本属性。使用NSLog我可以看到我正在获取值并将它们正确传递给UILabels。但是详细视图未显示其UILabel中传递的值。
The strange thing is that I CAN set the detail view's title property correctly, just not the UILabel properties, which is very confusing!
奇怪的是,我可以正确设置详细视图的title属性,而不是UILabel属性,这非常令人困惑!
Here's my code:
这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
if (self.detailView == nil) {
DetailController *detail = [[DetailController alloc] initWithNibName:@"DetailController" bundle:nil];
self.detailView = detail;
[detail release];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[theList getItem:cell.textLabel.text]];
[self.detailView.theID setText:@"LOOK AT ME NOW!"];
self.detailView.thePrice.text = [[dict objectForKey:@"PRICE"] stringValue];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:self.detailView animated:YES];
NSString *newTitle = [[NSString alloc] initWithString: @"Viewing Attributes for: "];
newTitle = [newTitle stringByAppendingString:cell.textLabel.text];
self.detailView.title = newTitle;
[dict release];
[newTitle release];
}
Thanks for any help on this! I love this community - you guys are awesome!
感谢您的帮助!我喜欢这个社区 - 你们真棒!
I Found An Answer! I think...
After much trial and error I was able to figure out a solution, but the solution doesn't entirely make sense to me and still leaves me with questions.
经过多次试验和错误,我能够找到解决方案,但解决方案对我来说并不完全合理,但仍然让我有疑问。
Siegfried suggested that I move my "pushViewController" message to the end of the method so that the last six lines of my code were changed to:
Siegfried建议我将“pushViewController”消息移到方法的末尾,以便我的代码的最后六行更改为:
// Pass the selected object to the new view controller.
NSString *newTitle = [[NSString alloc] initWithString: @"Viewing Attributes for: "];
newTitle = [newTitle stringByAppendingString:cell.textLabel.text];
self.detailView.title = newTitle;
[dict release];
[newTitle release];
[self.navigationController pushViewController:self.detailView animated:YES];
}
When I moved it to the last line (as shown above) my program would crash with an EXC_BAD_ACCESS error. I still don't understand why moving that line of code to the end of my method caused the program to crash?
当我将它移动到最后一行时(如上所示),我的程序会因EXC_BAD_ACCESS错误而崩溃。我仍然不明白为什么将这行代码移到我的方法的末尾会导致程序崩溃?
However, this got me thinking - I was able to successfully change the title of the new detail view, but not the view's UILabel properties. So I moved my pushViewController method back to where it was and then moved my methods for setting the new view's UILabels underneath the pushViewController method and BOOM! Everything worked! The new detail view was being pushed when the user clicked on the row in my table and its properties (title and UILabels) were all being set correctly.
但是,这让我思考 - 我能够成功更改新详细视图的标题,但不能更改视图的UILabel属性。所以我将我的pushViewController方法移回原来的位置,然后移动我的方法在pushViewController方法和BOOM下设置新视图的UILabel!一切正常!当用户单击表格中的行并且其属性(标题和UILabels)都正确设置时,正在推送新的详细信息视图。
Why does this work?
I remember reading about how the iPhone handles views, something called "lazy loading" or "lazy instantiation"? I think this means that I can't change the properties of my detail view UNTIL I've "pushed" it, which is when the object is actually created in memory and its properties become available?
我记得读过iPhone如何处理视图,称为“延迟加载”或“懒惰实例化”?我认为这意味着我无法更改我的详细信息视图的属性UNTIL我已经“推”它,这是当对象实际在内存中创建并且其属性可用时?
If this is correct, that would kind of explain the error I was getting when I moved the pushViewController method to the end of my code - I was trying to change the title property on a view that wasn't instantiated. However, that doesn't completely make sense because I was trying to change the UILabel properties of the view in my original code, which was before the view was actually pushed and the program wasn't crashing?
如果这是正确的,这可以解释当我将pushViewController方法移动到我的代码末尾时我得到的错误 - 我试图更改未实例化的视图上的title属性。但是,这并不完全有意义,因为我试图在原始代码中更改视图的UILabel属性,这是在实际推送视图之前,程序没有崩溃?
So my code that is currently working looks like the following:
所以我目前正在运行的代码如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
if (self.detailView == nil) {
DetailController *detail = [[DetailController alloc] initWithNibName:@"DetailController" bundle:nil];
self.detailView = detail;
[detail release];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[theList getItem:cell.textLabel.text]];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:self.detailView animated:YES];
// Moved the "set property" methods to happen after the detail view is pushed
[self.detailView.theID setText:@"LOOK AT ME NOW!"];
self.detailView.thePrice.text = [[dict objectForKey:@"PRICE"] stringValue];
NSString *newTitle = [[NSString alloc] initWithString: @"Viewing Attributes for: "];
newTitle = [newTitle stringByAppendingString:cell.textLabel.text];
self.detailView.title = newTitle;
[dict release];
[newTitle release];
}
I like that the code is working, but I would really like to understand what was and is actually happening. Can anyone offer additional insight here?
我喜欢代码正在运行,但我真的很想了解实际发生了什么。任何人都可以提供更多见解吗?
Thank you!
谢谢!
2 个解决方案
#1
2
Move
移动
[self.navigationController pushViewController:self.detailView animated:YES];
to last line.
到最后一行。
#2
0
You may try doing:
你可以尝试做:
NSString *strText = [NSString stringWithFormat:@"%@",[[dict objectForKey:@"PRICE"] stringValue]];
self.detailView.thePrice.text = [NSString stringWithFormat:@"%@",strText];
Check wheather strText is containing the expected value and is not nil.
检查天气strText是否包含预期值并且不是零。
#1
2
Move
移动
[self.navigationController pushViewController:self.detailView animated:YES];
to last line.
到最后一行。
#2
0
You may try doing:
你可以尝试做:
NSString *strText = [NSString stringWithFormat:@"%@",[[dict objectForKey:@"PRICE"] stringValue]];
self.detailView.thePrice.text = [NSString stringWithFormat:@"%@",strText];
Check wheather strText is containing the expected value and is not nil.
检查天气strText是否包含预期值并且不是零。