2013-08-19 2 views
-1

self를 뷰 컨트롤러에 대한 포인터로 사용하지만 antoher 클래스 (nsobject 클래스)의 속성에 자체를 할당하려고하지만 속성은 항상 nil입니다. 여러분 중 누구라도 왜 또는 어떻게 해결할 수 있는지 알고 있습니까?ios가 다른 클래스의 속성에 self를 할당합니다.

viewcontroller.m

-(void)startToDoSomething:(NSString*)testToRun 
{ 
    SecondClass *secondClass = [[SecondClass alloc] init]; 
    secondClass.viewController = self; 
    [secondClass doSomething]; 
} 

SecondClass.h :

NSObject의 CLASSE :

.H 파일

#import "ViewController.h" 

@class ViewController; 

@interface SecondClass : NSObject 
{ 
    ViewController *viewController; 
} 

@property (nonatomic,retain) ViewController *viewController; 
+0

'viewController'가'SecondClass'에서 어떻게 선언/구현 되었습니까? –

+0

secondClass가 선언되고 (#import secondClass) 속성이 secondClass.h에 선언됩니다. – HelenaM

+0

secondclass.h 내용을 추가했습니다. – HelenaM

답변

2

왜 init 메소드를 추가하지 않습니다

를 ,210

그럼 그냥 당신은 또한 당신이 _viewController를 사용 SecondClass 내부 @synthesize viewController = _viewController

에 합성을 (nonatomic, assign)에 속성을 변경하고 변경이

SecondClass *secondClass = [[SecondClass alloc] initWithViewController:self]; 

처럼 호출 할 수 있습니다.

희망이 있습니다.