I have a photo taking app that has a PhotoViewController with the code below for notifications. 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.
The PhotoViewVC.h contains:
@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:
- (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:
Why does _photoBestCount start at 0 every time and not continually increase?
In step 10 you state:
PhotoViewVC via delegation creates another instance (third) of PhotoViewVC.
Every time you create a new VC you're creating a new iVar _photoBestCount
, initialized to zero. So if you want a persistent counter, just put this property in a Model class (call it PhotoCounter, or whatever). Create an instance of this class and pass it into PhotoViewVC. Now, every time you update this iVar you're using the same counter, not a new one
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.