了解NSNotifications并更新属性

时间:2015-03-30 06:27:04

标签: ios uinavigationcontroller delegates nsnotificationcenter nsnotifications

我有一个带有PhotoViewController的照片应用程序,其下面的代码用于通知。此视图控制器上还有一个按钮,用于启动照片拍摄视图控制器,该控制器以模态方式打开,并通过委派在PhotoViewController中显示。

PhotoViewVC.h包含:

@interface PhotoViewVC : UIViewController <UITextViewDelegate, PhotoTakingVCDelegate>

// Photo data
@property (nonatomic, strong) NSData *photoData;
@property (nonatomic, strong) NSString *photoText;
@property (nonatomic, strong) NSString *photoObject;
@property (nonatomic, strong) NSString *photoParentObject;
@property (nonatomic, strong) NSString *photoUsername;
@property (nonatomic, strong) NSNumber *photoBestCount;
@property (nonatomic, strong) NSNumber *photoReplyCount;
@property (nonatomic, strong) NSMutableAttributedString *photoLabel;
@property (nonatomic, assign) BOOL photoViewerAddedBests;

@end

PhotoViewVC.m有:

- (void) viewDidLoad
{
    [self postNotification];
    [self listenToNotifications];
}

- (void) postNotification
{        
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:_photoObject forKey:@"photoObject"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"replyHappened" object:self userInfo:dataDict];
}

- (void) listenToNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleReplied:) name:@"replyHappened" object:nil];
}

- (void) handleReplied:(NSNotification *) note
{
    NSDictionary *data = [note userInfo];

    if (data != nil)
    {
        NSString *object = [data objectForKey:@"photoObject"];

        // Update counts
        int count = [_photoBestCount intValue];

        count = count + 1;

        _photoBestCount = [NSNumber numberWithInt:count];

        _bestsCount.text = [NSString stringWithFormat:@"%@", _photoBestCount];
    }
}

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

// Now the PhotoTakingVC delegation functions:

- (void) photoTakingVCDismiss:(PostViewController *) sender
{
    [sender dismissViewControllerAnimated:YES completion:nil];
}

- (void) photoTakingVCPresent:(PostViewController *) sender
{
    [self dismissViewControllerAnimated:YES completion:nil];

    PhotoViewVC *best = [[PhotoViewVC alloc] init];

    best.photoData = _photoData;
    best.photoText = _photoText;
    best.photoObject = _photoObject;
    best.photoParentObject = _photoParentObject;
    best.photoUsername = _photoUsername;
    best.photoBestCount = _photoBestCount;
    best.photoReplyCount = _photoReplyCount;
    best.photoLabel = _photoLabel;
    best.photoViewerAddedBests = _photoViewerAddedBests;

    best.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:best animated:NO];
}

- (void) photoView:(NSData *) photo andText:(NSString *) text andObject:(NSString *) object andParentObject:(NSString *) parentObject andUsername:(NSString *) username andBestCount:(NSNumber *) bestCount andReplyCount:(NSNumber *) replyCount andLabel:(NSMutableAttributedString *) label andAddedBests:(BOOL) addedBests
{
    _photoData = photo;
    _photoText = text;
    _photoObject = object;
    _photoParentObject = parentObject;
    _photoUsername = username;
    _photoBestCount = bestCount;
    _photoReplyCount = replyCount;
    _photoLabel = label;
    _photoViewerAddedBests = addedBests;
}

我无法使我的计数增加。所以我的应用程序是这样的:

  1. PhotoTakingVC启动并创建所有最佳计数(初始为0)。
  2. 正确加载PhotoViewVC,最佳计数为0.
  3. 用户在PhotoViewVC上,点击回复按钮。
  4. PhotoTakingVC弹出并拍照。
  5. PhotoViewVC为新照片创建自己的新实例。
  6. 用户点击第二个PhotoViewVC实例上的按钮,返回第一个实例。
  7. Bests count设置为1,这是正确的。
  8. 在PhotoViewVC的第一个实例上使用再次点击回复。
  9. 再次PhotoTakingVC会弹出并保存照片及其数据。
  10. PhotoViewVC通过委托创建PhotoViewVC的另一个实例(第三个)。
  11. 用户点击此第三个实例返回第一个实例。
  12. 最佳计数仍为1,应为2。
  13. 为什么_photoBestCount每次都从0开始而不是不断增加?

1 个答案:

答案 0 :(得分:0)

在步骤10中,您声明:

  

PhotoViewVC通过委托创建PhotoViewVC的另一个实例(第三个)。

每次创建新VC时,您都要创建一个新的iVar _photoBestCount,初始化为零。因此,如果你想要一个持久性计数器,只需将此属性放在Model类中(称之为PhotoCounter,或其他)。创建此类的实例并将其传递给PhotoViewVC。现在,每次更新此iVar时,您都使用相同的计数器,而不是新计数器