2012-12-28 35 views
0

我有一個名爲「Htgg」的plist填充的表格視圖。 我試圖將值推送到一個名爲「DetailViewController」的新視圖。當單元格被挖掘時將值推送到新的ViewController

這是代碼:

#import "Glist.h" 

#import "DetailViewController.h" 

@implementation Glist 
@synthesize htgg,detailViewController; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"ofek", @"ofek"); 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    self.navigationItem.title = @"איך להוציא גימלים?"; 
    NSString *htggFile = [[NSBundle mainBundle]pathForResource:@"Htgg" ofType:@"plist"]; 
    htgg = [[NSDictionary alloc] initWithContentsOfFile: htggFile]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [htgg count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    id keys = [htgg allKeys]; 


    //Get all the keys in the first dictionary (the keys are: "good", "funny", "New - Item 3") 
    //This is an array, so you can do this: array[0] (in C#) 

    //Here we tell the tableview cell. to put in the text - [keys objectsAtIndex:indexPath.row]; if the row is 0, we will get: "good", if 1, we will get "funny" 
    cell.textLabel.text = [keys objectAtIndex:indexPath.row]; 


    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailViewController *showPickedSolution = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 
    [self.navigationController pushViewController:showPickedSolution animated:YES]; 


    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
@end 

代碼工作完美,但它並沒有推動該行的值(plist中的所有行字符串型及以上的有「根」)。

我需要你的幫助將值推送到另一個視圖(DetailViewController)。

回答

1

使你的相同類型的DetailViewController屬性爲你的數據,你要推,然後用

DetailViewController *showPickedSolution = [[DetailViewController alloc] 
initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 
showPickedSolution.yourPropertName = [htgg objectAt:indexPath.Row];  

[self.navigationController pushViewController:showPickedSolution animated:YES]; 

您現在可以訪問你的財產傳遞DAT在DetailViewController

請檢查語法。

0

要達到此目的,您可以在DetailViewController類中創建NSString屬性,並在didSelectRowAtIndexPath中設置該屬性。這是給你參考代碼:

showPickedSolution.valueToBeSent = [htgg valueForKey:[[htgg allKeys] objectAtIndex:indexPath.row]]; 

您可以在DetailViewController類使用此valueToBeSent

0

我在兩個不同的方式具體取決於該方案做到了這一點:

1)代表,當我填補我想傳遞迴原來的屏幕在窗體上的一些價值觀。

2)故事板Segue,當我想發送一些值到第二個屏幕。

我通常把所有我想要的值放在一個數組中以發送該對象,然後在到達時提取我需要的東西。

我可以參考iOS開發站點的Getting Started部分,而不是單方面解釋。

我希望它有幫助!