所以我试图使用本教程iphonedevsdk在两个视图之间传递数据(第一个视图是 tableViewController,当按下单元格时数据发送到第二个视图,第二个视图得到 imageView,当数据发送时显示图像) 。
我在我的视图中使用相同的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
theAppDataObject.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)
我的问题是 theAppDataObject.imageString 始终为空
我知道的可能的解决方案:
不要将 AppDataObject 用作通用数据容器,而只是将数据保存在 appDelegate 中。
前任。:
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.imageString = tempImageString;
但我想弄清楚如何使用协议。
我尝试了什么:使 DataObject 全局化:
视图1.h
@interface championsListTableViewController : UITableViewController
{
NSString *tempImageString;
AppDataObject* theDataObject;
}
@property(strong,nonatomic) NSString *tempImageString;
@property(strong,nonatomic) AppDataObject* theDataObject;
NSLog 的输出(@"theDataObject is %@",theDataObject); :
theDataObject 为 (null),这怎么可能?