2015-03-03 19 views
0

我有一個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的出現進行測試,並且它輸出的山的名字到日誌,但運行應用程序時,標籤保持空白。

任何人都可以請幫忙嗎? 謝謝..!

回答

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; 

現在你的屬性設置爲你想要設置的名稱。現在 在viewDidLoad中的MountainInformation.m

-(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]; 
} 
+0

太謝謝你了!我必須保留uinavigationcontroller代碼,因爲我的視圖已連接到一個,但現在它可以從您的代碼中運行。 非常感謝你:) – Gethin 2015-03-03 12:21:29

+0

沒問題,請接受答案,以便如果有人遇到同樣的問題,他們可以幫助自己的答案,歡呼 – 2015-03-03 12:22:16

+0

會做:)它告訴我,我必須等待幾分鐘,但預先。 .. – Gethin 2015-03-03 12:23:59

相關問題