Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

iphone - passing data between views

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...