[英]iphone UIViewController does not show data passed from the first view controller
該應用程序的目的是檢索在UITableCell
上選擇的數據,並將其傳遞給新的視圖控制器。 一切正常,直到用戶選擇了單元格。 下一個視圖控制器已加載,但屏幕為黑色。 在該屏幕上,我有一個UIImageView
和UILabel
。 我在情節UINavigationController
板上設置了UINavigationController
。 我正在嘗試使在第一個屏幕上選擇的圖像和文本到達下一個屏幕上的各個對象。 我已經從數據庫中檢索數據並將其加載到第一個屏幕。
這是我的storedetail.h
上的代碼,它是UIViewController
的子類,
@property (nonatomic, copy) UIImage *storeImage;
@property (nonatomic, copy) UILabel *storeCode;
這是我在其中調用nextviewcontroller的代碼,它是ShowStores.m
,它是UITableView
的子類,
標頭代碼
#import "ShowStores.h"
#import "StoreDetail.h"
當用戶選擇單元格時我加載下一個視圖的代碼,
(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];
}
我已經調試了它,並且cell.textLabel.text
和cell.imageView.image
不是nil或null。 第二個視圖加載,但為黑色。 我在第二個視圖上設置了一個后退按鈕,它將用戶帶回到第一個視圖,並且效果很好。
UIImage和UILabel不符合NSCopying協議,這很可能是原因。 因此,聲明@property(非原子,副本)不會對您的意圖產生任何影響。
我的建議是為它們設置保留,然后在要制作副本的地方,只需初始化對象並將對象分配給它們。
帶有storeCode uiLabel的第二個視圖控制器是否有一個筆尖? 還是在viewDidLoad中以編程方式創建storeCode UILabel?
我將刪除此行:
storedetail.storeCode = cell.textLabel;
在這一行:
storedetail.storeImage = cell.imageView.image;
當我認為您希望它成為UIImageView時,storeImage是一個UIImage。 (我也將刪除該行,並以與創建storeCode uiLabel相同的方式創建一個storeImageView-將其放在筆尖中或在viewDidLoad中以編程方式創建它。
編輯:
在viewDidLoad中創建對象,如下所示:
storeCode = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 120, 16)];
[self addSubview:storeCode];
對於圖像,我將圖像的名稱(文件名或路徑)設置為第二個視圖控制器上的NSString屬性,然后在viewWillAppear中使用字符串創建正確的UIImage,然后將該UIImage分配給UIImageView。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.