简体   繁体   English

使用SharedSingleton将值归因于NSInteger

[英]Using SharedSingleton to attribute value to NSInteger

In my viewcontrollerA.h I have: 在我的viewcontrollerA.h中,我有:

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

in viewcontrollerA.m I use: 在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;
}

...to find the value of the integer. ...查找整数值。

In viewcontrollerB I import ViewcontrollerA.h, and in ViewcontorllerB.m, I attribute a value to showCommentOrCreate. 在viewcontrollerB中,我导入ViewcontrollerA.h,在ViewcontorllerB.m中,我将值赋予showCommentOrCreate。

The only problem is that it seams I have to assign the value to the integer twice for it to change. 唯一的问题是它接缝了,我必须将值赋给整数两次才能更改。 eg: 例如:

Doesn't work: 不起作用:

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

PhotoViewController* sharedSingleton2 = [PhotoViewController sharedManager];
sharedSingleton2.showCommentOrCreate = 2;

}

Doesn't work: 不起作用:

-(IBAction)addAnImageForCommenting:(id)sender{

PhotoViewController* sharedSingleton2 = [PhotoViewController sharedManager];
sharedSingleton2.showCommentOrCreate = 2;

}

Works: 作品:

- (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];
}}}

As you can see I set the value of the integer twice, and it works, otherwise it doesn't. 如您所见,我将整数的值设置了两次,并且它可以工作,否则就不能。 When It doesn't, if i call the code twice with a time interval, the right value is set. 如果没有,则如果我每隔一段时间调用两次代码,则会设置正确的值。 Any ideas?? 有任何想法吗??

EDIT: Updated the code I claim to work 编辑:更新了我声称可以工作的代码

I think the problem comes from using int's as a primitive variable rather than the NSInteger which is an entire integer class. 我认为问题出在使用int作为基本变量,而不是使用NSInteger(它是整个整数类)。

To solve this my idea would be to allocate the NSInteger and work with that, but as NSInteger doesn't work that way I'd suggest to go with NSNumber as it behaves entirely as an object 为了解决这个问题,我的想法是分配NSInteger并使用它,但是由于NSInteger不能那样工作,我建议使用NSNumber,因为它完全像一个对象一样工作

 sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:1];

I am so stupid. 我真傻 I'm very sorry. 我非常抱歉。 I didn't even realize that i was setting the value in ViewcontrollerB after allocating UIviewcontrollerA, and the call to that value in UIviewcontrollerA is in the view did load. 我什至没有意识到我在分配UIviewcontrollerA之后在ViewcontrollerB中设置了值,并且在视图中确实加载了对UIviewcontrollerA中的该值的调用。 This fixed it: 这修复了它:

-(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);

}

Instead of... 代替...

-(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];

}

...again i'm sorry 再次对不起

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

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