Reputation: 3451
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?
Upvotes: 3
Views: 1461
Reputation: 475
In a class if you want to use a protocal you can use like as :
// this the way too declare a protocal.
// .h file
@protocol TestProtocol <NSObject>
-(void)testMyProtocolMethod:(NSString *)testvalue;
@end
@interface TestProtocolClass: NSObject
{
id <TestProtocol> delegate;
}
@property (nonatomic , assign) id <TestProtocol> 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 <TestProtocol>{
}
@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
Upvotes: 1
Reputation: 107121
First Check theAppDataObject
is null or not.
If it is null then:
Write AppDataObject* theDataObject;
in your inteface and declare a property as strong
If theDelegate.theAppDataObject
is returning null , allocate that object first.
If it is not null then:
Change this line theAppDataObject.imageString = tempImageString;
to
theAppDataObject.imageString = [tempImageString retain];
If you are using ARC set the property of imageString
to strong
.
or Check with this
theAppDataObject.imageString = [[NSString alloc] initWthString:tempImageString];
Upvotes: 2
Reputation: 475
In any class where you want to set any variable of the "AppDelegate Class", you can try this, this will surely solve your problem.
//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;
Upvotes: 0