将值传递给另一个UIViewController

时间:2013-09-29 10:02:28

标签: objective-c uiviewcontroller nsstring

我正在尝试将值(UIImage,NSString)传递给另一个ViewController,但它无法正常工作。

我的代码如下所示:

1st ViewController.m

#import 2nd ViewController.h

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AppDetail *advc = [[AppDetail alloc] init];
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        advc.appTitel = name;
        advc.appIcon = icon;
        advc.detailAppName = detileName;
        advc.appDescription = description;
    }
}

第二次ViewController.h

#import <UIKit/UIKit.h>

@interface AppDetail : UIViewController

@property (strong, nonatomic) NSString *appTitel;
@property (strong, nonatomic) UIImage *appIcon;
@property (strong, nonatomic) NSString *detailAppName;
@property (strong, nonatomic) NSString *appDescription;

@end

2nd ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.appTitel;
    self.appIconImageView.image = self.appIcon;
    self.detailAppNameTextView.text = self.detailAppName;
    self.appDescriptionTextView.text = self.appDescription;
}

但我总是得到(null)所有值!

我做错了什么?

2 个答案:

答案 0 :(得分:2)

由此纠正:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

       // Get reference to the destination view controller
        AppDetail *advcc = [segue destinationViewController];

        advc.appTitel = name;
        advc.appIcon = icon;
        advc.detailAppName = detileName;
        advc.appDescription = description;
    }
}

当您不使用故事板时,它下面的代码:

AppDetail *advc = [[AppDetail alloc] init];

答案 1 :(得分:1)

纠正这些行

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //AppDetail *advc = [[AppDetail alloc] init];
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        AppDetail *advc     = segue.destinationViewController; //ADD THIS
        advc.appTitel       = name;
        advc.appIcon        = icon;
        advc.detailAppName  = detileName;
        advc.appDescription = description;
    }
}