需要表视图详细视图的帮助。我似乎无法将值写入 UILabel 的文本属性?
我有一个 TableView 设置作为我的“根视图控制器”和一个名为“DetailController”的详细视图控制器,以允许我显示用户在主表视图中选择的内容的详细视图。导航工作正常 - 当我在表格中选择一行时,屏幕向左滑动,并显示详细信息视图,但我无法更新详细信息视图的 UILabel 文本属性。就像它只是忽略我的 UILable 设置属性方法(参见下面的代码),因为它没有给我任何错误或警告。
我已经测试过,当我点击“tableView:didSelectRowAtIndexPath:”方法时,我确实拥有所需的值,但由于某种原因,我似乎无法更新详细信息视图的 UILabels 文本属性。使用 NSLog,我可以看到我正在获取值并将它们正确地传递给 UILabels。但是,详细信息视图未在其 UILabels 中显示传递的值。
奇怪的是,我可以正确设置详细视图的标题属性,但不能正确设置 UILabel 属性,这非常令人困惑!
这是我的代码:
- (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];
}
感谢您对此的帮助!我喜欢这个社区——你们太棒了!
我找到了答案!我认为......
经过多次尝试和错误,我能够找到解决方案,但该解决方案对我来说并不完全有意义,并且仍然给我留下了疑问。
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];
}
当我将其移至最后一行(如上所示)时,我的程序将因 EXC_BAD_ACCESS 错误而崩溃。我仍然不明白为什么将该行代码移到我的方法末尾会导致程序崩溃?
然而,这让我思考 - 我能够成功更改新详细信息视图的标题,但不能更改视图的 UILabel 属性。因此,我将 PushViewController 方法移回到原来的位置,然后将用于设置新视图的 UILabels 的方法移到了 PushViewController 方法下方,然后 BOOM!一切顺利!当用户单击我的表中的行时,将推送新的详细信息视图,并且其属性(标题和 UILabels)均已正确设置。
为什么这有效?
我记得读过有关 iPhone 如何处理视图的内容,称为“延迟加载”或“延迟实例化”?我认为这意味着我无法更改详细视图的属性,直到我“推送”它,即对象实际在内存中创建并且其属性变得可用时?
如果这是正确的,那就可以解释当我将 PushViewController 方法移动到代码末尾时遇到的错误 - 我试图更改未实例化的视图上的标题属性。但是,这并不完全有意义,因为我试图在原始代码中更改视图的 UILabel 属性,这是在视图实际推送并且程序没有崩溃之前?
因此,我当前正在运行的代码如下所示:
- (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 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.
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.
The strange thing is that I CAN set the detail view's title property correctly, just not the UILabel properties, which is very confusing!
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:
// 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?
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.
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?
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?
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试这样做:
检查 strText 是否包含预期值并且不为零。
You may try doing:
Check wheather strText is containing the expected value and is not nil.
移至
最后一行。
Move
to last line.