2013-05-11 1 views
0

내가 더 후 1 개 클래스의 객체를 사용하지만 하나 개의 클래스에 initWithNibName를 사용하는 경우 내가의 getInstance :

static IGMapViewController *instance =nil; 

+(IGMapViewController *)getInstance { 
    @synchronized(self) { 
     if (instance==nil) { 
      instance= [IGMapViewController new]; 
     } 
    } 
    return instance; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // more code 
     instance = self; 
    } 

    return self; 
} 

이 클래스는 점에서

IGMapViewController 이름이있다.

init 메소드의 IGRouteController 클래스에서 _mapViewController = [IGMapViewController getInstance];을 사용하면 initWithNibName이 다른 클래스에서 실행되기 전에 발생합니다. IGRouteController에서

은 내가 사용하는 방법 방법 updateRouteList 있습니다 : 그것은 모든 실행 않습니다

[_mapViewController drawSuggestedRoute:suggestedRoute];

을하지만 결과를 볼 수 없습니다.

은 내가 사용하는 경우 :

IGMapViewController *wtf = [IGMapViewController getInstance]; 
[wtf drawSuggestedRoute:suggestedRoute]; 

는 그럼 잘 작동 않습니다.

인스턴스와 초기화를 나중에 사용할 수 있습니까?

+3

달성하려는 목표는 무엇입니까? –

+0

인스턴스를 어딘가에 가져옵니다.initWithNib 및 make는 인스턴스를 initWithNib로 만든 인스턴스와 같게 만듭니다. – clankill3r

+0

필자는보기 컨트롤러 싱글 톤을 사용하려고 시도한 앱을 사용해 보았습니다. 불쾌한데, 무엇을하려합니까? –

답변

0

나는 당신이 성취하려는 것을보고 있습니다. 펜촉에서 클래스의 싱글 톤 인스턴스를 초기화하려고합니다. 옳은?

인스턴스를 초기화 할 때 의도 한 동작이 아닌 [IGMapViewController new]을 사용하고 있습니다. 이건 어때?

+ (id)sharedController 
{ 
    static dispatch_once_t pred; 
    static IGMapViewController *cSharedInstance = nil; 

    dispatch_once(&pred, ^{ 
     cSharedInstance = [[self alloc] initWithNibName:@"YourNibName" bundle:nil]; 
    }); 
    return cSharedInstance; 
} 
0

clankill3r, 당신은 (이 토론 UIViewController as a singleton에서 볼 댓글) 싱글 UIViewController의를 생성하지 않도록해야

. 이것은 @ CarlVeazey으로도 강조되었습니다.

IMHO, 필요할 때마다 UIViewController을 만들어야합니다. 이 경우보기 컨트롤러는 재사용 가능한 구성 요소가됩니다. 컨트롤러의 새로운 인스턴스를 만들 때 속성이나 이니셜 라이저에 관심있는 데이터를이 경우 suggestedRoute을 삽입하면됩니다. 추가 정보에 대한 UIViewController들, 정말 조언 @Ole Begemann에 의해 두 개의 흥미로운 게시물을 읽을 수

// YourViewController.h 
- (id)initWithSuggestedRoute:(id)theSuggestedRoute; 

// YourViewController.m 
- (id)initWithSuggestedRoute:(id)theSuggestedRoute 
{ 
    self = [super initWithNibName:@"YourViewController" bundle:nil]; 
    if (self) { 
     // set the internal suggested route, e.g. 
     _suggestedRoute = theSuggestedRoute; // without ARC enabled _suggestedRoute = [theSuggestedRoute retain]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self drawSuggestedRoute:[self suggestedRoute]]; 
} 

:

간단한 예는 다음이 될 수 있습니다. Passing Data Between View Controllers

  • initWithNibName:bundle: Breaks Encapsulation
  • 도움이되기를 바랍니다

    • .

    관련 문제