[Objective-C]将值传递到另一个 ViewControler

发布时间: 2016/9/22 5:31:28
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图将值 (UIImage,NSString) 传递到另一个 ViewController,但它不会工作。

我的代码看起来像这样︰

1 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

第二届 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) 的所有值 !

我到底做错了??

解决方法 1:

通过这来更正︰

- (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;
    }
}

它下面的代码是当您不使用 storyboard :

AppDetail *advc = [[AppDetail alloc] init];
赞助商