0

IOS 5 と Storyboard コーディングを使用しています。検索バーでsqliteデータベースに接続されたtableViewを持つアプリケーションを構築しました。行に触れると、「詳細」と呼ばれる別のビュー コントローラーに自動的に移動します。テーブル ビューから詳細ビュー コントローラーにデータを渡す必要があります。たとえば、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
4

2 に答える 2

0

はい、使用する必要があります

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

メソッド (オーバーライド) ウィッチを使用すると、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 を使用して、データ モデルから関連データを取得します。

お役に立てれば!

于 2012-07-18T07:12:37.947 に答える