在视图之间传递数据 [英] passing data between views

查看:78
本文介绍了在视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在我的观点中使用相同的 theAppDataObject 方法:

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

按下单元格时我正在尝试发送数据 theAppDataObject.imageString = tempImageString;

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输出

tempIm-g = Champ_0.jpg

tempIm-g = Champ_0.jpg

theAppDataObject.imageString =(null)

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)

temp image = (null)

我的问题是appDataObject.imageString总是为空的

My problem is that theAppDataObject.imageString is always null

我知道可能的解决方案:

Possible solutions that i know:

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

Do not use AppDataObject as generic data container and just save data in appDelegate.

例如:

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

但我想知道如何使用协议。

But i want to figure out how to use protocols.

我尝试过:
使theDataObject全局化:

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;

输出(@theDataObject is%@,theDataObject) ; :

output of NSLog(@"theDataObject is %@",theDataObject); :

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

theDataObject is (null), how is this possible?

推荐答案

首先检查 theAppDataObject 是否为空。

如果为null则为:

在您的界面中写入 AppDataObject * theDataObject; 并将属性声明为 strong

Write AppDataObject* theDataObject; in your inteface and declare a property as strong

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

If theDelegate.theAppDataObject is returning null , allocate that object first.

如果它不为null则:

If it is not null then:

更改此行 theAppDataObject.imageString = tempImageString;

theAppDataObject.imageString = [tempImageString retain];

如果您使用ARC设置 imageString strong

If you are using ARC set the property of imageString to strong.

或查看此

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

这篇关于在视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆