在视图之间传递数据

所以我试图在两个视图之间传递数据(第一个视图是tableViewController,当按下单元格数据被发送到第二个视图,第二个视图得到imageView,当数据被发送时显示图像)使用本教程iphonedevsdk 。

我在我的视图中使用相同的AppDataObject方法:

- (AppDataObject*) theAppDataObject { id theDelegate = (id) [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输出:

temp image =(null)

我的问题是AppDataObject.imageString始终为null


我知道可能的解决方案:

不要将AppDataObject用作通用数据容器,只需将数据保存在appDelegate中。

例:

 AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; appdelegate.imageString = tempImageString; 

但我想弄清楚如何使用协议。

我尝试了什么:使theDataObject全局:

view1.h

 @interface championsListTableViewController : UITableViewController { NSString *tempImageString; AppDataObject* theDataObject; } @property(strong,nonatomic) NSString *tempImageString; @property(strong,nonatomic) AppDataObject* theDataObject; 

NSLog的输出 (@“theDataObject is%@”,theDataObject); :

theDataObject是(null),这怎么可能?

首先检查theAppDataObject是否为null。

如果为null则为:

AppDataObject* theDataObject; 在你的界面中并声明一个strong的财产

如果theDelegate.theAppDataObject返回null,则首先分配该对象。

如果它不为null则:

更改此行theAppDataObject.imageString = tempImageString;

 theAppDataObject.imageString = [tempImageString retain]; 

如果使用ARC, imageString的属性设置为strong

或检查一下

theAppDataObject.imageString = [[NSString alloc] initWthString:tempImageString];

在课堂上,如果你想使用一个protocal,你可以使用如下:

  // this the way too declare a protocal. // .h file @protocol TestProtocol  -(void)testMyProtocolMethod:(NSString *)testvalue; @end @interface TestProtocolClass: NSObject { id  delegate; } @property (nonatomic , assign) id  delegate; /* synthesize in the .m file of this class*/ @end //Now you have to use this protocol in any class where you want to use , Do like as: //let us suppose you want to use this protocal method in a class named "DemoProtocal". // .h file import "TestProtocol.h" @interface DemoProtocal { } @end //.m file #import "DemoProtocal.h" @implementation DemoProtocal - (id)init{ TestProtocol *test = [[TestProtocol alloc]init]; test.delegate = self; } -(void)testMyProtocolMethod:(NSString *)testvalue{ // Do appropriate things. } @end 

在任何你想要设置“AppDelegate Class”变量的类中,你可以尝试这个,这肯定会解决你的问题。

  //Declare a variable from where you want to set the value of the "AppDelegate Class". //in the yourclass.h file. AppDelegate/* this is the main class of the Application*/ *appDel; //and initialize it where you want to use or you can initialize at the init of the class. appDel = (AppDelegate *)[[UIApplication sharedApplication]delegate]; // Then set the value. appDel.imageString = tempImageString; 
Interesting Posts