Übergeben von Daten zwischen Ansichten
Ich versuche also, Daten zwischen zwei Ansichten zu übertragen (die erste Ansicht ist tableViewController, wenn die Zelle gedrückt wird, werden Daten an die zweite Ansicht gesendet, die zweite Ansicht hat imageView, wenn die Daten gesendet werden, wird das Bild angezeigt)iphonedevsdk.
Ich benutze das gleichetheAppDataObject Methode aus meiner Sicht:
- (AppDataObject*) theAppDataObject
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
AppDataObject* theDataObject;
theDataObject = (AppDataObject*) theDelegate.theAppDataObject;
return theDataObject;
}
Wenn die Zelle gedrückt wird, versuche ich, Daten zu sendentheAppDataObject.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-Ausgabe:
tempIm-g = Champ_0.jpg
theAppDataObject.imageString = (null)
SecondViewController (Bild anzeigen):
-(void)viewWillAppear:(BOOL)animated
{
AppDataObject* theDataObject = [self theAppDataObject];
UIImage *tempImage = [UIImage imageNamed:theDataObject.imageString];
NSLog(@"temp image = %@",tempImage);
[choosenChampionImageView setImage:tempImage];
}
NSLog-Ausgabe:
temporäres Bild = (null)
Mein Problem ist, dass theAppDataObject.imageString immer null ist
Mögliche Lösungen, die ich kenne:
Verwenden Sie AppDataObject nicht als generischen Datencontainer und speichern Sie die Daten nur in appDelegate.
Ex.:
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.imageString = tempImageString;
Aber ich möchte herausfinden, wie man Protokolle benutzt.
Was ich versucht habe: Make theDataObject global:
view1.h
@interface championsListTableViewController : UITableViewController
{
NSString *tempImageString;
AppDataObject* theDataObject;
}
@property(strong,nonatomic) NSString *tempImageString;
@property(strong,nonatomic) AppDataObject* theDataObject;
Ausgabe von NSLog (@ "theDataObject ist% @", theDataObject); :
theDataObject ist (null), wie ist das möglich?