将值传递给另一个UIViewController

问题描述 投票:0回答:2

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

我的代码看起来像这样:

第一个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)

我究竟做错了什么??

objective-c uiviewcontroller nsstring
2个回答
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
投票

纠正这些线条

- (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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.