So i’m trying to pass data between two views (first view is tableViewController,when cell is pressed data is send to second view,second view got imageView, when data is send image is shown) using this tutorial iphonedevsdk.
I’m using same theAppDataObject method in my views:
- (AppDataObject*) theAppDataObject
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
AppDataObject* theDataObject;
theDataObject = (AppDataObject*) theDelegate.theAppDataObject;
return theDataObject;
}
When cell is pressed i’m trying to send data 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 output:
tempIm-g = Champ_0.jpg
theAppDataObject.imageString = (null)
SecondViewController (show image):
-(void)viewWillAppear:(BOOL)animated
{
AppDataObject* theDataObject = [self theAppDataObject];
UIImage *tempImage = [UIImage imageNamed:theDataObject.imageString];
NSLog(@"temp image = %@",tempImage);
[choosenChampionImageView setImage:tempImage];
}
NSLog output:
temp image = (null)
My problem is that theAppDataObject.imageString is always null
Possible solutions that i know:
Do not use AppDataObject as generic data container and just save data in appDelegate.
Ex.:
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.imageString = tempImageString;
But i want to figure out how to use protocols.
What i tried:
Make theDataObject global:
view1.h
@interface championsListTableViewController : UITableViewController
{
NSString *tempImageString;
AppDataObject* theDataObject;
}
@property(strong,nonatomic) NSString *tempImageString;
@property(strong,nonatomic) AppDataObject* theDataObject;
output of NSLog(@”theDataObject is %@”,theDataObject); :
theDataObject is (null), how is this possible?
First Check
theAppDataObjectis null or not.If it is null then:
Write
AppDataObject* theDataObject;in your inteface and declare a property asstrongIf
theDelegate.theAppDataObjectis returning null , allocate that object first.If it is not null then:
Change this line
theAppDataObject.imageString = tempImageString;to
If you are using ARC set the property of
imageStringtostrong.or Check with this
theAppDataObject.imageString = [[NSString alloc] initWthString:tempImageString];