簡體   English   中英

輕按單元格時將值推送到新的ViewController

[英]Push the values to new ViewController when a cell is tapped

我有一個名為“ Htgg”的plist填充的表視圖。 我試圖將值推送到一個名為“ DetailViewController”的新視圖。

這是代碼:

#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的所有行都是字符串類型,並且上面有'Root')。

我需要您的幫助才能將值推到另一個視圖(DetailViewController)。

在您的DetailViewController中創建與您要推送的數據類型相同的屬性,然后使用

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

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

您現在可以在屬性的DetailViewController中訪問傳遞的數據。

請檢查語法。

為此,您可以在類DetailViewController創建一個NSString屬性,並在didSelectRowAtIndexPath設置該屬性。 這是供您參考的代碼:

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

您可以在DetailViewController類中使用此valueToBeSent

我根據情況以兩種不同的方式完成了此操作:

1)委托,當我在要傳遞回原始屏幕的表單上填寫一些值時。

2)情節提要Segue,當我想向第二個屏幕發送一些值時。

我通常將所有想要的值放入數組中以發送對象,然后在到達時提取所需的內容。

除了向您介紹每種方法外,我還可以向您介紹iOS開發站點的入門部分。

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM