【问题标题】:passing data between views在视图之间传递数据
【发布时间】:2012-09-29 11:29:33
【问题描述】:

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

我在我的视图中使用相同的 theAppDataObject 方法:

- (AppDataObject*) theAppDataObject
{
    id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
    AppDataObject* theDataObject;
    theDataObject = (AppDataObject*) theDelegate.theAppDataObject;
    return theDataObject;
}

按下单元格时,我正在尝试发送数据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

AppDataObject.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 输出:

临时图像 = (null)

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


我知道的可能解决方案:

不要将 AppDataObject 用作​​通用数据容器,而只是将数据保存在 appDelegate 中。

例如:

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

但我想弄清楚如何使用协议。

我尝试了什么: 使 DataObject 全局化:

view1.h

@interface championsListTableViewController : UITableViewController
{
    NSString *tempImageString;

    AppDataObject* theDataObject;
}

@property(strong,nonatomic) NSString *tempImageString;

@property(strong,nonatomic) AppDataObject* theDataObject;

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

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

【问题讨论】:

  • 你检查我的答案了吗? AppDataObject* theDataObject; 在您的代码中是本地的,这就是问题所在。写在你的界面中
  • 您是否删除了 theDataObject 的本地声明?
  • 我确定这是 retutnin null theDelegate.theAppDataObject
  • 是的,它返回 null,我知道这是一个愚蠢的错误,但我错过了 appDelegate 中 theAppDataObject 的初始化
  • 是的,我知道,这就是我更新答案的原因。无论如何,很高兴知道您的问题已得到解决。

标签: iphone objective-c ios xcode


【解决方案1】:

首先检查theAppDataObject 是否为空。

如果为空则:

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

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

如果不为空则:

更改此行theAppDataObject.imageString = tempImageString;

到

theAppDataObject.imageString = [tempImageString retain];

如果您使用 ARC,请将 imageString 的属性设置为 strong。

或检查此

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

【讨论】:

  • 这是不可能的,因为我正在使用 ARC
  • @anonymous:imageString 的属性是什么?强吗?
  • @anonymous:我已经更新了答案,检查最后一个选项。它会工作
  • AppDataObject.imageString 的值始终为空
  • 分配的对象也返回 null!这不可能。您需要检查theAppDataObject 是否为空
【解决方案2】:

如果你想在一个类中使用一个协议,你可以像这样使用:

  // 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

【讨论】:

    【解决方案3】:

    在你想设置“AppDelegate Class”的任何变量的任何类中,你可以试试这个,这一定会解决你的问题。

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

    【讨论】:

    • 谢谢,但正如我已经说过的,我知道这是可行的,只是我想弄清楚如何使用协议
    猜你喜欢
    • 2014-06-18
    • 2022-01-07
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多