我有一个tableview设置来显示来自sqlite数据库的数据,现在我要做的是设置一个视图,以便在用户点击表格单元格时显示来自所述数据库的更多信息。
从我到目前为止所读到的,我的理解是这主要是在表视图控制器内的prepareForSegue方法中完成的?
无论如何,经过大量的谷歌搜索后,我最终得到了一个AppCoda教程(link) ...
if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *nav = segue.destinationViewController;
MountainInformation *destViewController = [nav.viewControllers objectAtIndex:0];
MountainsObject *mountain = [self.mountains objectAtIndex:indexPath.row];
destViewController.nameLabel = mountain.name;
NSLog(@"%@", mountain.name);
}
我把NSLog放在那里测试,它确实将山的名字输出到日志中,但是当我运行应用程序时标签仍然是空白。
有人可以帮忙吗? 谢谢..!
答案 0 :(得分:1)
可以稍微缩短你的代码..
if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
MountainInformation *destViewController = [segue destinationViewController];// do this only if your segue is connected to the next one from previous, or else let it stay as you have, only change the later part
MountainsObject *mountain = [self.mountains objectAtIndex:indexPath.row];
destViewController.myMountain = mountain.name;
NSLog(@"%@", mountain.name);
}
现在在MountainInformation.h文件中创建一个属性
@property (nonatomic, strong) NSString* myMountain;
现在您的属性设置为您要设置的名称。 现在在MountainInformation.m的viewDidLoad中
-(viewDidLoad){
self.labelToShowName.text = self.myMountain;
}
希望这有帮助
[编辑] 在MountainInformation.h中导入您的山地类。
#import "MountainObject.h"
将属性更改为
@property(nonatomic, strong) MountainObject *selectedMountain
现在在以前的视图控制器中。
if([segue.identifier isEqualToString:@"pushToMountainInfo"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *nav = segue.destinationViewController;
MountainObject *mountain = [self.mountain objectAtIndex.indexPath.row];
destViewController.selectedMountain = mountain;
}
在MountainInformation.h中
-viewDidLoad{
NSString *mountainName = self.selectedMountain.name;
NSString *mountainRegion =self.selectedMountain.region;
NSString *mountainHeight =self.selectedMountain.height;
//and other properties. and then you can set it to a single label by using
self.myLabel.text = [NSString stringWithFormat:@" Name - %@, Height- %@, Region- %@",mountainName, mountainHeight, mountainRegion];
}