NSMutableIndexSet中的书签

时间:2014-03-05 18:48:08

标签: iphone ios7 xcode5

尝试在NSMutableIndexSet中保存书签页面,当点击按钮时,它会更改bookmarkButton图像,当翻页时它会更新bookmarkButton图像,但当我返回书签页面检查书签页面时,它不会显示书签页面上的书签-Y图像。这意味着它没有保存书签页面。我该如何解决?

 - (void)buttonTapped:(id)sender {

if(isFirstTime == YES){

  [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];

  [self performSelector:@selector(SaveBookmark:) withObject:contentViewController.page afterDelay:0];

  isFirstTime = NO;

} else {

   [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-N.png"] forState:UIControlStateNormal];

    isFirstTime = YES;
}
       }

- (void)SaveBookmark: (id)sender{

bookmarkIndex =[[NSMutableIndexSet indexSet] init];

[bookmarkIndex addIndex:currentIndex];

 BOOL Bookmarked = [bookmarkIndex containsIndex:currentIndex];
 }


 - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

if (!completed)
{
    return;
}
currentIndex = [modelArray indexOfObject:contentViewController.page];

[self displaycurrentIndex:currentIndex];

[self updatebookmarks:currentIndex];

 }


- (void) updatebookmarks:(NSUInteger)currentIndex {
if (Bookmarked==YES){


    [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];

} else {
  [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-N.png"] forState:UIControlStateNormal];

}
   }

知道在NSMutableIndexSet中保存书签时我做错了什么。将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

我在这里看到一些问题。我会尝试解决所有问题:

bookmarkIndex = [[NSMutableIndexSet indexSet] init]; - 这是不正确的

应该是:[NSMutableIndexSet indexSet][[NSMutableIndexSet alloc] init]

接下来,正如@Romain所说,你应该将它初始化为类初始化或viewDidLoad方法中的属性,无论它是否解决了这个问题。每次添加时都不应重新初始化该集。

接下来,我不确定为什么在调用save方法时使用performSelector:withObject:afterDelay。这有什么理由吗?创建saveBookmark方法的更好方法是:

- (BOOL)saveBookmark
{
   if (!bookmarkIndex) {
      bookmarkIndex = [NSMutableIndexSet indexSet];
   }
   if (currentIndex) {
      [bookmarkIndex addIndex:currentIndex];
      return [bookmarkIndex containsIndex:currentIndex];
   }
   return NO;
}

这样您就可以检查saveBookmark方法的返回值。如果它返回'NO',那么你知道currentIndex有问题,或者你的bookmarkIndex变量正以负面方式受到程序另一部分的影响。在你的buttonTapped:方法中,然后你会保存这样的书签:

if (isFirstTime == YES) {
   [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];
   if ([self saveBookmark]) {
      isFirstTime = NO;
   } else {
      NSLog(@"There was an error saving the bookmark");
   }
} else {  ...code continues

如果不出意外,这至少可以帮助您诊断问题并追踪您的问题!

编辑:

如果使用上面的代码,则实际上是禁止使用该布尔值来确定页面是否已加入书签。在你的updateBookmarks:方法调用中,你应该检查当前索引的bookmarkIndex集,如下所示:

- (void) updatebookmarks:(NSUInteger)currentIndex {
   if ([bookmarkIndex containsIndex:currentIndex]) {
      [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-Y.png"] forState:UIControlStateNormal];
   } else {
      [_bookmarkButton setImage:[UIImage imageNamed:@"Bookmark-N.png"] forState:UIControlStateNormal];
   }
}