3

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

我在我的视图中使用相同的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

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

临时图像 = (null)

我的问题是 theAppDataObject.imageString 始终为空


我知道的可能的解决方案:

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

前任。:

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

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

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

视图1.h

@interface championsListTableViewController : UITableViewController
{
    NSString *tempImageString;

    AppDataObject* theDataObject;
}

@property(strong,nonatomic) NSString *tempImageString;

@property(strong,nonatomic) AppDataObject* theDataObject;

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

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

4

3 回答 3

2

First Check 是否theAppDataObject 为空。

如果它为空,则:

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

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

如果它不为空,那么:

更改此行theAppDataObject.imageString = tempImageString;

theAppDataObject.imageString = [tempImageString retain];

如果您使用 ARC,请将 的属性设置imageStringstrong

或检查此

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

于 2012-09-29T11:39:25.753 回答
1

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

  // 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
于 2012-09-29T13:26:04.197 回答
0

在任何你想设置“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;
于 2012-09-29T11:48:15.737 回答