简体   繁体   English

iPhone UIViewController不显示从第一个视图控制器传递的数据

[英]iphone UIViewController does not show data passed from the first view controller

The purpose of this app is to retrieve the data selected on UITableCell and pass that on to a new view controller. 该应用程序的目的是检索在UITableCell上选择的数据,并将其传递给新的视图控制器。 All is well until the user selects the cell. 一切正常,直到用户选择了单元格。 The next view controller is loaded but the screen is black. 下一个视图控制器已加载,但屏幕为黑色。 On that screen i have a UIImageView and UILabel . 在该屏幕上,我有一个UIImageViewUILabel I have a UINavigationController set up in the storyboard. 我在情节UINavigationController板上设置了UINavigationController I am trying to get the image and text selected on the first screen to the respective objects on the next screen. 我正在尝试使在第一个屏幕上选择的图像和文本到达下一个屏幕上的各个对象。 I have retrieved the data from a database and have loaded it to the first screen. 我已经从数据库中检索数据并将其加载到第一个屏幕。

Here is my code on my storedetail.h which is a subclass to a UIViewController , 这是我的storedetail.h上的代码,它是UIViewController的子类,

@property (nonatomic, copy) UIImage *storeImage;
@property (nonatomic, copy) UILabel *storeCode;

Here is my code where i am calling the nextviewcontroller which is ShowStores.m which is a subclass of a UITableView , 这是我在其中调用nextviewcontroller的代码,它是ShowStores.m ,它是UITableView的子类,

header code , 标头代码

#import "ShowStores.h"
#import "StoreDetail.h"

code where i load the next view when the user selects the cell, 当用户选择单元格时我加载下一个视图的代码,

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
StoreDetail *storedetail = [[StoreDetail alloc] init];
storedetail.storeImage = cell.imageView.image;
storedetail.storeCode = cell.textLabel;
storedetail.storeCode.text = cell.textLabel.text;
[self.navigationController pushViewController:storedetail animated:YES];
}

I have debugged it and the cell.textLabel.text and cell.imageView.image are not nil or null. 我已经调试了它,并且cell.textLabel.textcell.imageView.image不是nil或null。 The second view loads but is black. 第二个视图加载,但为黑色。 I have set up a back button on the second view which takes the user back to the first view and that works fine. 我在第二个视图上设置了一个后退按钮,它将用户带回到第一个视图,并且效果很好。

UIImage and UILabel don't conform to NSCopying protocol, that's most probably the reason. UIImage和UILabel不符合NSCopying协议,这很可能是原因。 And so declaring @property (nonatomic, copy) doesn't do any effect as your intention. 因此,声明@property(非原子,副本)不会对您的意图产生任何影响。

My suggestion is setting retain for them, and at the point where you want to make copies, just initialize and assign the objects to them. 我的建议是为它们设置保留,然后在要制作副本的地方,只需初始化对象并将对象分配给它们。

do you have a nib for your second view controller with the storeCode uiLabel in? 带有storeCode uiLabel的第二个视图控制器是否有一个笔尖? or are you creating the storeCode UILabel programmatically in viewDidLoad? 还是在viewDidLoad中以编程方式创建storeCode UILabel?

I would remove this line: 我将删除此行:

   storedetail.storeCode = cell.textLabel;

in this line: 在这一行:

   storedetail.storeImage = cell.imageView.image;

storeImage is a UIImage when I think you want it to be a UIImageView. 当我认为您希望它成为UIImageView时,storeImage是一个UIImage。 (I would remove that line too and create a storeImageView the same way as you create the storeCode uiLabel - either have it in your nib or create it programmatically in viewDidLoad. (我也将删除该行,并以与创建storeCode uiLabel相同的方式创建一个storeImageView-将其放在笔尖中或在viewDidLoad中以编程方式创建它。

EDIT: 编辑:

create the objects in viewDidLoad as follows: 在viewDidLoad中创建对象,如下所示:

storeCode = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 120, 16)];
[self addSubview:storeCode];

For the image I would set the name of the image (filename or path) to an NSString property on your second view controller and then in viewWillAppear use the string to create the correct UIImage and then assign that UIImage to your UIImageView. 对于图像,我将图像的名称(文件名或路径)设置为第二个视图控制器上的NSString属性,然后在viewWillAppear中使用字符串创建正确的UIImage,然后将该UIImage分配给UIImageView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从子视图iPhone向视图控制器传递数据 - Pass data to view controller from subview iPhone iPhone 上的 UISplitViewController:从细节视图控制器弹出/显示主视图 - UISplitViewController on iPhone: pop to / show primary view from detail view controller UIViewController容器是否必须是根视图控制器? - Does a UIViewController Container have to be the root view controller? 将UIViewcontroller的视图添加到UIWindow不会显示 - adding UIViewcontroller's view to UIWindow does not show 从模态视图 controller 推动 UIViewController - Pushing a UIViewController from a modal view controller iPhone Dev:Tab视图控制器。 第一个视图上的按钮不显示“文件所有者”中的操作 - iPhone Dev: Tab View controller. Buttons on first view don't show actions in File's Owner iPhone / UIViewController:离开视图时,处理数据输入的最佳方法是什么 - iPhone/UIViewController: when navigating away from a view, what is the best way to handle data entries 在iPhone上的Objective-C中将数据从委托发送到视图控制器 - Sending data from a delegate to a view controller in Objective-C on an iPhone 从iPhone上的弹出视图控制器传回数据 - Pass data back from a popover view controller on iPhone iPhone UIViewController以编程方式转换为UINavigation控制器 - Iphone UIViewController to UINavigation controller programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM