2012-09-29 3 views
3

그래서 두 개의보기간에 데이터를 전달하려고합니다. 첫 번째보기는 셀을 눌렀을 때 두 번째보기로 보내고 두 번째보기는 imageView가 있고 데이터가 이미지를 보내는 경우 표시됩니다. 자습서 iphonedevsdk.보기간에 데이터 전달

- (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 출력을 :

내보기에 같은 theAppDataObject 방법을 사용하고

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은 항상 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 "theDataObject는 %의 @입니다"); :

theDataObject는 (null)입니다. 어떻게 가능합니까?

+0

당신이 내 대답을 확인 했습니까? 'AppDataObject * theDataObject;'는 코드에서 지역적인 문제입니다. 인터페이스에 쓰기 –

+0

DataObject의 로컬 선언을 제거 했습니까? –

+0

네, 그것을 제거했습니다 – ignotusverum

답변

2

첫 번째 확인 theAppDataObject이 null인지 아닌지 확인하십시오.

그 다음 null의 경우 :

쓰기 AppDataObject* theDataObject; 당신의 inteface을과 theDelegate.theAppDataObject가 null 반환하는 경우 strong

같은 속성을 선언, 먼저 객체를 할당합니다.

그 다음 NULL이 아닌 경우

변경이 라인 theAppDataObject.imageString = tempImageString;

theAppDataObject.imageString = [tempImageString retain]; 

에 당신은 ARC는 strongimageString의 속성을 설정 사용하는 경우. 당신이 "AppDelegate에 클래스"의 변수를 설정하려는 모든 클래스에서이

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

+0

ARC를 사용하고 있기 때문에 불가능합니다. – ignotusverum

+0

@anonymous :'imageString'의 속성은 무엇입니까? 그것은 강한가요? –

+0

예, imageString의 속성이 우수합니다. – ignotusverum

0

또는 확인이 시도 할 수 있습니다, 이것은 반드시 문제를 해결할 것입니다.

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

감사하지만, 이미 제가 알고있는 것처럼 말입니다. 프로토콜 사용법 알아보기 – ignotusverum

1
클래스에서

당신은 당신이 같은처럼 사용할 수있는 protocal을 사용하려면 :

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