简体   繁体   English

了解NSNotifications和更新属性

[英]Understanding NSNotifications and updating a property

I have a photo taking app that has a PhotoViewController with the code below for notifications. 我有一个照相应用程序,其中有一个PhotoViewController,下面的代码用于通知。 There is also a button on this view controller that lauches the photo taking view controller which is opened modally and is presented in the PhotoViewController via delegation. 该视图控制器上还有一个按钮,用于启动拍照视图控制器,该按钮已打开并通过委托显示在PhotoViewController中。

The PhotoViewVC.h contains: 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

The PhotoViewVC.m has: 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;
}

I am having trouble making my counts increase. 我无法增加计数。 So my app works like this: 所以我的应用程序是这样的:

  1. PhotoTakingVC launches and creates all the best counts (initally 0). PhotoTakingVC启动并创建所有最佳计数(最初为0)。
  2. PhotoViewVC is loaded correctly and best count is 0. PhotoViewVC已正确加载,最佳计数为0。
  3. User is on PhotoViewVC and clicks reply button. 用户在PhotoViewVC上,然后单击“答复”按钮。
  4. PhotoTakingVC pops up and takes photo. PhotoTakingVC弹出并拍照。
  5. PhotoViewVC creates a new instance of itself for new photo. PhotoViewVC为新照片创建其自身的新实例。
  6. User hits back button on second instance of PhotoViewVC and goes back to first instance. 用户在PhotoViewVC的第二个实例上单击后退按钮,然后返回到第一个实例。
  7. Bests count gets set to 1 which is correct. 最佳计数被设置为1,这是正确的。
  8. Use on first instance of PhotoViewVC hits reply again. 在PhotoViewVC的第一个实例上使用再次击中回复。
  9. Again PhotoTakingVC pops up and saves photo and its data. 再次弹出PhotoTakingVC并保存照片及其数据。
  10. PhotoViewVC via delegation creates another instance (third) of PhotoViewVC. PhotoViewVC通过委派创建了PhotoViewVC的另一个实例(第三个实例)。
  11. User clicks back on this third instance to go back to first instance. 用户单击此第三实例将返回到第一实例。
  12. Best count still is one and should be 2. 最佳计数仍为1,应为2。

Why does _photoBestCount start at 0 every time and not continually increase? 为什么_photoBestCount每次都从0开始而不连续增加?

In step 10 you state: 在步骤10中,您指出:

PhotoViewVC via delegation creates another instance (third) of PhotoViewVC. PhotoViewVC通过委派创建了PhotoViewVC的另一个实例(第三个实例)。

Every time you create a new VC you're creating a new iVar _photoBestCount , initialized to zero. 每次创建新的VC时,您都在创建一个新的iVar _photoBestCount ,并将其初始化为零。 So if you want a persistent counter, just put this property in a Model class (call it PhotoCounter, or whatever). 因此,如果您想要一个持久计数器,只需将此属性放在Model类中(称为PhotoCounter或其他名称)即可。 Create an instance of this class and pass it into PhotoViewVC. 创建此类的实例,并将其传递到PhotoViewVC。 Now, every time you update this iVar you're using the same counter, not a new one 现在,每次更新此iVar时,您使用的是同一计数器,而不是新计数器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM