将值传递给另一个UIViewController
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将值传递给另一个UIViewController相关的知识,希望对你有一定的参考价值。
我试图将值(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)
!
我究竟做错了什么??
答案
正确的是:
- (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];
另一答案
纠正这些线条
- (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;
}
}
以上是关于将值传递给另一个UIViewController的主要内容,如果未能解决你的问题,请参考以下文章