使用SharedSingleton将值赋给NSInteger

在我的viewcontrollerA.h中我有:

@property (nonatomic, assign) NSInteger showCommentOrCreate; + (PhotoViewController *) sharedManager; 

在viewcontrollerA.m我使用:

 PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; ...(long)sharedSingleton.showCommentOrCreate... + (PhotoViewController *)sharedManager { static PhotoViewController *shaderManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shaderManager = [[PhotoViewController alloc] init]; }); return shaderManager; } 

…find整数的值。

在viewcontrollerB我导入ViewcontrollerA.h,并在ViewcontorllerB.m,我属性的值showCommentOrCreate。

唯一的问题是,它接缝,我必须将值分配给整数两次,以改变。 例如:

不起作用:

 -(IBAction)addAnImageForCommenting:(id)sender{ PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; sharedSingleton.showCommentOrCreate = 2; PhotoViewController* sharedSingleton2 = [PhotoViewController sharedManager]; sharedSingleton2.showCommentOrCreate = 2; } 

不起作用:

 -(IBAction)addAnImageForCommenting:(id)sender{ PhotoViewController* sharedSingleton2 = [PhotoViewController sharedManager]; sharedSingleton2.showCommentOrCreate = 2; } 

作品:

 - (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer { for (UIImageView *imageView in imageArray) { if (([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UITapGestureRecognizer *)gestureRecognizer).view.tag)) { // for(UIGestureRecognizer *gesture in [imageView gestureRecognizers]){ // if([gesture isKindOfClass:[UITapGestureRecognizer class]]){ if (imageView.frame.size.height == 60){ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.27]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.frame = CGRectMake( 20, imageView.frame.origin.y, 710, 200); [UIView commitAnimations]; [imageView setImage:[UIImage imageNamed: @"Massages retina iPad.png"]]; z = 200; for (MKMapView* map in mapViewArray) { if (imageView.tag == map.tag +1) { [imageView addSubview:map]; } } }else { /*[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.27]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.frame = CGRectMake( 20, imageView.frame.origin.y, 710, 60); [UIView commitAnimations]; [imageView setImage:[UIImage imageNamed: @"message small.png"]];*/ z = 60; for (MKMapView* map in mapViewArray) { if (imageView.tag == map.tag +1) { //[map removeFromSuperview]; } } } } else if([imageView isKindOfClass:[UIImageView class]] && imageView.tag > ((UITapGestureRecognizer *)gestureRecognizer).view.tag){ if (z == 200){ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.27]; imageView.frame = CGRectMake( 20, imageView.frame.origin.y +150, 710, imageView.frame.size.height); [UIView commitAnimations]; }else { /*[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.27]; imageView.frame = CGRectMake( 20, imageView.frame.origin.y -150, 710, imageView.frame.size.height); [UIView commitAnimations];*/ } }} for (UIImageView *imageView in imageArray) { if (([imageView isKindOfClass:[UIImageView class]] && imageView.tag == ((UISwipeGestureRecognizer *)gestureRecognizer).view.tag)) { [UIView transitionWithView:imageView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ //imageView.image = secondImage; } completion:^(BOOL f){ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PhotoViewControllerStoryboard" bundle:nil]; UIViewController *vc = [sb instantiateInitialViewController]; vc.modalTransitionStyle = UIViewAnimationOptionCurveEaseIn; vc.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:vc animated:YES]; vc.view.superview.frame = CGRectMake(15, 43, 735, 982); PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; sharedSingleton.tagNumber = imageView.tag; //NSLog(@"The tagNumber is: %ld", (long)sharedSingleton.tagNumber); //sharedSingleton.showCommentOrCreate = 1; }]; PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; sharedSingleton.tagNumber = imageView.tag; sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:1]; }}} 

正如你所看到的,我设置整数的值两次,它的工作,否则它不。 当它不,如果我以一个时间间隔两次调用代码,正确的值被设置。 有任何想法吗??

编辑:更新我声称工作的代码

我认为问题来自使用int作为一个原始variables,而不是整个整数类的NSInteger。

为了解决这个问题,我的想法是分配NSInteger并使用它,但是因为NSInteger不能这样工作,所以我build议使用NSNumber,因为它完全作为一个对象

  sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:1]; 

我很愚蠢。 我非常抱歉。 我甚至没有意识到我在分配UIviewcontrollerA之后在ViewcontrollerB中设置了值,并且在UIviewcontrollerA中对该值的调用是在视图中加载的。 这固定了它:

 -(IBAction)addAnImageForCommenting:(id)sender{ PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:2]; UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PhotoViewControllerStoryboard" bundle:nil]; UIViewController *vc = [sb instantiateInitialViewController]; vc.modalTransitionStyle = UIViewAnimationOptionCurveEaseIn; vc.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:vc animated:YES]; vc.view.superview.frame = CGRectMake(15, 43, 735, 982); } 

代替…

 -(IBAction)addAnImageForCommenting:(id)sender{ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PhotoViewControllerStoryboard" bundle:nil]; UIViewController *vc = [sb instantiateInitialViewController]; vc.modalTransitionStyle = UIViewAnimationOptionCurveEaseIn; vc.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:vc animated:YES]; vc.view.superview.frame = CGRectMake(15, 43, 735, 982); PhotoViewController* sharedSingleton = [PhotoViewController sharedManager]; sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:2]; } 

再次对不起