2011-09-28 5 views
0

저는 iPad 앱에서 일하고 있습니다. 사용자가 검색 할 수있는 페이지가 많습니다. 나는 그들이 나중에 다시 방문 할 수 있도록 사용자가 페이지를 '좋아할'수 있도록 기능을 구현하고 있습니다.알림을 유포하기 위해 (목록을 관리하는) 클래스에 적합한 방법은 무엇입니까?

나는 사용자가 좋아하는 페이지 목록을 관리하는 즐겨 찾기 클래스를 작성하고 있습니다. 사용자가 '즐겨 찾기'버튼을 누르면 Favorites 클래스의 addFavorite : 또는 removeFavorite : 메서드를 호출합니다. 입력은 충분히 단순 해 보입니다.

제 질문은 : 상태 변경 이벤트를 내 모든 의견에 전파하는 가장 좋은 방법은 무엇입니까? 나는 많은 중복 '좋아하는'표시기가 응용 프로그램을 통해 흩어져 있고, 그들은 모두 동기화 유지해야합니다. 예를 들어 사용자가 분홍색 플로이드를 한 번에 즐겨 찾기 (별색을 회색에서 노란색으로 변경)하면 분홍색 플로이드에 연결되는 다른 모든보기는 회색 대신 링크 옆에 노란색 별표를 표시해야합니다.

Objective-C 알림을 사용하여이 작업을 수행하는 방법은 많이 있다는 것을 알고 있습니다. 나는 청결하고 유지 보수가 가능한 것을 찾고 있습니다. 과거에 당신에게 효과가 있었던 것은 무엇입니까? 시간 내 줘서 고마워.

답변

1

NSNotificationCenterNSNotification을 확인하십시오. 대리인 패턴을 어렵게 만드는 공유 정보에 관심있는 사람이 두 명 이상인 경우 특히 정기적으로 알림을 사용합니다. 내가 만난 알림의 주된 문제는 UITableViewCell을 알림에 등록 할 때입니다. 대기열에있는 셀이 알림에 응답하지 않도록합니다.

0

다음은 NSNotificationCenter 및 보너스 포인트 용 UITableView를 사용하여 작성한 코드입니다. 어쩌면 그것은 다른 누군가를 돕는 역할을 할 것입니다. 즐겨 찾기 클래스에서

: 테이블보기, 각 셀 스타가 있습니다 : 즐겨 찾기와 상호 작용하는 3 개보기

+ (void)toggleFavorite:(NSString *)artistName { 
    if([favorites member:artistName]) { 
     [favorites removeObject:artistName]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"favoriteRemoved" 
                  object:artistName]; 
    } else { 
     [favorites addObject:artistName]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"favoriteAdded" 
                  object:artistName]; 
    } 

    [[UserLibrary current] verifyLibrary]; 
} 

// register for notifications 
- (void)awakeFromNib { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(favoriteAdded:) 
               name:@"favoriteAdded" 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(favoriteRemoved:) 
               name:@"favoriteRemoved" 
               object:nil]; 
} 

// search through visible cells for the one that needs to be starred 
- (void)favoriteAdded:(NSNotification *)notification { 
    NSString *artistName = notification.object; 
    for(ArtistTableViewCell *cell in [(UITableView*)self.view visibleCells]) { 
     if([artistName isEqualToString:cell.artistLabel.text]) { 
      cell.starred = YES; 
     } 
    } 
} 

// search through visible cells for the one that needs to be de-starred 
- (void)favoriteRemoved:(NSNotification *)notification { 
    NSString *artistName = notification.object; 
    for(ArtistTableViewCell *cell in [(UITableView*)self.view visibleCells]) { 
     if([artistName isEqualToString:cell.artistLabel.text]) { 
      cell.starred = NO; 
     } 
    } 
} 

// when cells are created or reused, make sure the star is set properly 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ArtistTableViewCell *cell = ... 
    NSString *name = ... 

    cell.starred = [Favorites isFavorite:name]; 
    return cell; 
} 

- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    [super dealloc]; 
} 

테이블의 셀입니다. 셀이 TableView에서 이벤트를 수신하여 알림에 등록 할 필요가 없으며 (대기열에서 제외 된 후 위험에 처할 수 있음) 알 수 있습니다.

- (IBAction)starPressed:(id)sender { 
    NSString *name = artistLabel.text; 
    [Favorites toggleFavorite:name]; 
} 

- (void)setStarred:(bool)isFavorite { 
    UIImage *img; 
    if(isFavorite) { 
     img = [UIImage imageNamed:@"filledstar30px"]; 
    } else { 
     img = [UIImage imageNamed:@"emptystar30px"]; 
    } 
    [favoriteButton setImage:img forState:UIControlStateNormal]; 
} 
관련 문제