所以我试图在两个视图之间传递数据(第一个视图是tableViewController,当按下单元格数据被发送到第二个视图,第二个视图获得imageView,当数据被发送时显示图像)使用本教程{{3 }}。
我在我的视图中使用了相同的 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输出:
temp image =(null)
我的问题是AppDataObject.imageString始终为null
我知道可能的解决方案:
不要将AppDataObject用作通用数据容器,只需在appDelegate中保存数据。
例:
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
appdelegate.imageString = tempImageString;
但我想弄清楚如何使用协议。
我尝试了什么: 使theDataObject成为全局:
view1.h
@interface championsListTableViewController : UITableViewController
{
NSString *tempImageString;
AppDataObject* theDataObject;
}
@property(strong,nonatomic) NSString *tempImageString;
@property(strong,nonatomic) AppDataObject* theDataObject;
NSLog的输出(@“theDataObject is%@”,theDataObject); :
theDataObject是(null),这怎么可能?
答案 0 :(得分:2)
首先检查theAppDataObject
是否为空。
如果为null则为:
在您的界面中写下AppDataObject* theDataObject;
并将属性声明为strong
如果theDelegate.theAppDataObject
返回null,则首先分配该对象。
如果它不为null,则:
更改此行theAppDataObject.imageString = tempImageString;
到
theAppDataObject.imageString = [tempImageString retain];
如果您使用ARC,请将imageString
的属性设置为strong
。
或使用此
进行检查 theAppDataObject.imageString = [[NSString alloc] initWthString:tempImageString];
答案 1 :(得分: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
答案 2 :(得分: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;