从TableView到DetailViewController

时间:2013-05-23 09:00:18

标签: iphone objective-c ios6 detailsview

我有一个带有一些名字的TableView。 我想在另一个ViewController上显示这些名称,但我不明白。

这是我的代码:

AnguckenViewController.m

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


UIViewController *DetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];


AnguckenViewController *DetailKey = [self.originalsource objectAtIndex:[tableView indexPathForSelectedRow].row];

const char *cPlainKey = [DetailKey cStringUsingEncoding:NSASCIIStringEncoding];

NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES) objectAtIndex:0];



NSString *pszDetailValueString = [NSString stringWithFormat:@"%s",szSafeKey];

NSString *szDetailValue = [temp objectForKey:pszDetailValueString];



[DetailViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:DetailViewController animated:YES completion:^(void){}];

}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController {
    IBOutlet UIButton *BackButton;
    NSString *_pszKey;
    NSString *_pszValue;
}
@property (nonatomic, retain) NSString *pszKey;
@property (nonatomic, retain) NSString *pszValue;

@property (retain, nonatomic) IBOutlet UILabel *keyLabel;
@property (retain, nonatomic) IBOutlet UILabel *valueLabel;


@end

DetailViewController.m

@implementation DetailViewController
@synthesize pszKey;
@synthesize pszValue;


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

- (void)viewDidLoad
{
    [super viewDidLoad];
        _keyLabel.text = _pszKey;
}

2 个答案:

答案 0 :(得分:0)

您需要设置视图控制器的pszKey属性,例如在以模态方式显示它之前,DetailViewController。

DetailViewController.pszKey = @"The value";
[DetailViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:DetailViewController animated:YES completion:^(void){}];

同样在viewDidLoad中,最好使用属性的getter而不是直接访问实例变量:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.keyLabel.text = self.pszKey;
}

答案 1 :(得分:0)

所以最后它有效,

我换了一行:

 UIViewController *DetailViewController = [self.storyboard     instantiateViewControllerWithIdentifier:@"DetailViewController"];

为:

DetailViewController *DetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

由于