【发布时间】:2012-09-29 11:29:33
【问题描述】:
所以我试图在两个视图之间传递数据(第一个视图是 tableViewController,当按下单元格时数据发送到第二个视图,第二个视图得到 imageView,当数据发送时显示图像)使用本教程 iphonedevsdk .
我在我的视图中使用相同的 theAppDataObject 方法:
- (AppDataObject*) theAppDataObject
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
AppDataObject* theDataObject;
theDataObject = (AppDataObject*) theDelegate.theAppDataObject;
return theDataObject;
}
按下单元格时,我正在尝试发送数据theAppDataObject.imageString = tempImageString;:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tempImageString = (((championList *) [self.champions objectAtIndex:indexPath.row]).championImage);
AppDataObject *theDataObject = [self theAppDataObject];
NSLog(@"tempIm-g = %@",tempImageString);
theDataObject.imageString = tempImageString;
NSLog(@"theAppDataObject.imageString = %@",theDataObject.imageString);
[self.navigationController popToRootViewControllerAnimated:YES];
}
NSLog 输出:
tempIm-g = Champ_0.jpg
AppDataObject.imageString = (null)
SecondViewController(显示图片):
-(void)viewWillAppear:(BOOL)animated
{
AppDataObject* theDataObject = [self theAppDataObject];
UIImage *tempImage = [UIImage imageNamed:theDataObject.imageString];
NSLog(@"temp image = %@",tempImage);
[choosenChampionImageView setImage:tempImage];
}
NSLog 输出:
临时图像 = (null)
我的问题是 AppDataObject.imageString 总是为空
我知道的可能解决方案:
不要将 AppDataObject 用作通用数据容器,而只是将数据保存在 appDelegate 中。
例如:
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.imageString = tempImageString;
但我想弄清楚如何使用协议。
我尝试了什么: 使 DataObject 全局化:
view1.h
@interface championsListTableViewController : UITableViewController
{
NSString *tempImageString;
AppDataObject* theDataObject;
}
@property(strong,nonatomic) NSString *tempImageString;
@property(strong,nonatomic) AppDataObject* theDataObject;
输出 of NSLog(@"theDataObject is %@",theDataObject); :
theDataObject 为 (null),这怎么可能?
【问题讨论】:
-
你检查我的答案了吗?
AppDataObject* theDataObject;在您的代码中是本地的,这就是问题所在。写在你的界面中 -
您是否删除了 theDataObject 的本地声明?
-
我确定这是 retutnin null
theDelegate.theAppDataObject -
是的,它返回 null,我知道这是一个愚蠢的错误,但我错过了 appDelegate 中 theAppDataObject 的初始化
-
是的,我知道,这就是我更新答案的原因。无论如何,很高兴知道您的问题已得到解决。
标签: iphone objective-c ios xcode