2010-02-03 3 views
1

objective-c (코코아 터치)에서 나는 전환중인 일련의 UIViewController가 있습니다.메서드 범위에서 "참조"변수 해제

- (void)switchViews:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UIViewController *nextViewController; 
    int tag = button.tag; 

    switch (tag) 
    { 
     // -- has already been created 
     case kFinancialButton: 
      nextViewController = financials; 
      break; 

     case kSocialButton: 
      if (social == nil) 
      { 
       SocialViewController *socialViewController = [[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil]; 
       social = socialViewController; 
       [socialViewController release]; 
      } 
      nextViewController = social; 
      break; 

     case kTicketingButton: 
      if (ticketing == nil) 
      { 
       TicketingViewController *ticketingViewController = [[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil]; 
       ticketing = ticketingViewController; 
       [ticketingViewController release]; 
      } 
      nextViewController = ticketing; 
      break; 
    } 

     /////// 
------> // -- [button/nextViewController release]???? 
     /////// 

    [self setActiveButton:button]; 
} 

볼 수 있듯이 볼 컨트롤러 중 하나를 "nextViewController"에 할당하고 있습니다. 내가이 "로컬"변수를 공개해야하거나 아니면 내 view 컨트롤러 중 하나 (dealloc에서 해제)를 가리키고 있기 때문에 혼자있는 상태로 두어도 괜찮은지 궁금합니다. "태그"는 "원시적"이기 때문에 공개해야한다고 생각하지 않습니다. 맞습니까? 버튼은 어때? 나는 명백하게 공개해야 할 것과 받아 들여서는 안되는 것을 잘 이해하지 못해서 나는 지나치게 조심스러워하고 있습니다. 미리 감사드립니다.

답변

1

일반적으로 retain 'init'또는 copy '일 수있는 변수는 release 일뿐입니다.

편집 : 나쁜 값으로 다른 문제가있을 것 같은

조금 더 코드를 읽은 후, 그것은 보인다. 아래의 코드는 나에게 조금 더 이해가됩니다. 이것은 금융, 사회 및 발권이 모두 @synthesized ivars라고 가정합니다.

- (void)switchViews:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UIViewController *nextViewController; 
    int tag = button.tag; 

    switch (tag) 
    { 
     // -- has already been created 
     case kFinancialButton: 
      nextViewController = self.financials; 
      break; 

     case kSocialButton: 
      if (!social) { 
       self.social = [[[SocialViewController alloc] initWithNibName:@"SocialViewController" bundle:nil] autorelease]; 
      } 
      nextViewController = self.social; 
      break; 

     case kTicketingButton: 
      if (!ticketing) { 
       self.ticketing = [[[TicketingViewController alloc] initWithNibName:@"TicketingViewController" bundle:nil] autorelease]; 
      } 
      nextViewController = self.ticketing; 
      break; 
    } 

    // Do something with nextViewController I'd assume 

    [self setActiveButton:button]; 
} 
+0

그래,이처럼 보이는 결국 : 사회 = [[SocialViewController의 ALLOC] initWithNibName : "SocialViewController"번들 @ : 무기 호] – typeoneerror

+0

나는 "자기"가 필요하지 않다고 생각합니다. 그래? 범위가 기본적으로 "자체"라고 가정합니다. 이리. 또한 autorelease를 사용하지 않았다. 왜냐하면이 메소드와 dealloc에서 명시 적으로 유지하고 해제했기 때문이다. 이거 괜찮아? 고마워, 브라이언! – typeoneerror

+0

글쎄 자기를 사용하는 이유. @synthesized setter가 메모리 관리를 처리 할 수 ​​있다는 것을 기억하십시오. 또한 그것들을 사용하면 KVO를 준수하게 만들지 만, 일부 데스크탑 개발과 마찬가지로 아이폰에는 큰 문제가되지 않을 수도 있습니다. –

관련 문제