簡體   English   中英

在視圖控制器之間傳遞數據:從uitableview到詳細信息視圖控制器

[英]Passing Data between View Controllers : from uitableview to a details view controller

我正在使用IOS 5和Storyboard編碼。 我建立了一個應用程序,其中有一個tableView通過搜索欄連接到sqlite數據庫。 當我們觸摸一行時,它將自動將我們帶到另一個名為“詳細信息”的視圖控制器。 我需要將數據從表視圖傳遞到詳細信息視圖控制器,例如將author.title傳遞給labelText.text字段。 有任何想法嗎?

編輯的問題:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSString *Title;
    Details *dv = (Details*)segue.destinationViewController;
    author.title = dv.labelText.text;
}

部分代碼:

//
//  Details.m
//  AuthorsApp
//
//  Created by georges ouyoun on 7/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Details.h"
#import "Author.h"


@interface Details ()

@end

@implementation Details

@synthesize labelText;
@synthesize selectedAuthors;
@synthesize author;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   self.labelText.text = self.author.title;
    // Do any additional setup after loading the view.
    NSLog(@"Everything is ok now !");
}

- (void)viewDidUnload
{
  //  [self setLabelText:nil];
    NSLog(@"U have entered view did unload");
    [super viewDidUnload];

    [self setLabelText:Nil];
    // Release any retained subviews of the main view.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSString *Title;
    Details *dv = (Details*)segue.destinationViewController;
  //  author.title = dv.labelText.text;
    dv.labelText.text = author.title;
}




/*
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"AuthorsCell"]) {

        [segue.destinationViewController setLabelText:author.title];

    }


}




/*
-(void)viewWillAppear:(BOOL)animated
{ 

    self.labelText.text = author.title;

    NSLog(@"U have entered the viewWillAppear tag");
  //  detailsLabel.text = food.description;
}
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)dealloc {
    [labelText release];
    [super dealloc];
}
@end

出於演示目的,我假設我們有兩個視圖控制器ListViewControllerDetailViewController ListViewController具有tableview,並且已經在segue between your tableview cell and details view controller創建了一個segue between your tableview cell and details view controller

我假設您將其命名為“ DetailSegue”。 您的情節提要板應類似於下圖。

在此處輸入圖片說明

在tableViewCell和DetailViewController之間選擇序列,打開屬性檢查器,並將標識符指定為'DetailSegue'。 見下圖,

在此處輸入圖片說明

現在運行應用程序,您應該會看到從ListViewController到DetailViewController的順利導航。

Assuming you datasource is an array of stings and you have your datasource and delegate setup already properly for your list tableView

在List視圖控制器中添加一個prepareForSegue:sender方法,每當segue即將執行時該方法都會被調用,該方法將被自動調用。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([segue.identifier isEqualToString:@"DetailSegue"]) {
    DetailViewController *detailVC = (DetailViewController*)segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender];
    detailVC.title = [self.source objectAtIndex:indexPath.row];
  }
}

在DetailViewController中添加一個名為title的屬性以存儲所選標題,並為UILabel添加另一個出口以在視圖中顯示該標題。

DetailViewController.h

#import <Foundation/Foundation.h>

@interface RecipeListViewController : NSObject

@property (weak, nonatomic) NSString *title;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

DetailViewController.m

@implementation RecipeListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = self.title;
}
@end

不要忘記連接電源插座。 我希望這能使您大致了解如何在視圖控制器之間傳遞數據。

是的,您需要使用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

方法(重寫)witch允許您通過獲取segue.destinationViewController來傳遞詳細信息,然后設置目標控制器的屬性。

例如

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
detailViewController *dv = (detailViewController*)segue.destinationViewController;
dv.attribute1 = dataFromTable;
}

例如,如果將表中的數據存儲在數組中,一種簡單的方法是獲取被推入的行的行號並將其設置為變量

int rowPressed;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

rowPressed = indexPath.row;

}

然后在PrepareForSegue中將數據傳遞給DetailViewController。 使用rowPressed從數據模型中獲取相關數據。

希望這可以幫助!

暫無
暫無

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

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