2011-09-28 110 views
0

我正在研究iPad應用程序,有許多頁面供用戶瀏覽。我正在爲用戶實現功能,以便「收藏」頁面,以便稍後再訪問它。一個班級(管理一個列表)傳播通知的好方法是什麼?

我在寫一個收藏夾類,它將管理用戶喜歡的頁面列表。當用戶按下'收藏夾'按鈕時,它將調用收藏夾類中的addFavorite:或removeFavorite:方法。輸入看起來很簡單。

我的問題是:將狀態改變事件傳播到所有視圖的最佳方式是什麼?我在應用程序中散佈了許多多餘的「最喜愛的」指標,並且它們都需要保持同步。例如,如果用戶在一個視圖中最喜歡Pink Floyd(將星形從灰色更改爲黃色),則鏈接到Pink Floyd的所有其他視圖都需要在鏈接旁邊顯示黃色星星,而不是灰色星星。

這是我的理解,有很多方法來使用Objective-C通知來做到這一點。我只是在尋找一個清潔和可維護的。過去爲你工作的是什麼?謝謝你的時間。

回答

1

檢查NSNotificationCenterNSNotification。我經常使用通知,特別是如果有多方對共享信息感興趣使代表模式變得困難。我遇到的通知的主要問題是訂閱UITableViewCell的通知:避免出列的單元格響應通知。

0

下面是我最終編寫的使用NSNotificationCenter和UITableView獲取積分的代碼。也許它會幫助別人。

在收藏類:

+ (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]; 
} 

之一,與收藏互動的3次:表視圖,其中每個單元都有一個明星:

// 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]; 
} 
相關問題