0

在我的 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;
}

...找到整数的值。

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

如您所见,我将整数的值设置了两次,它有效,否则无效。如果没有,如果我以时间间隔调用代码两次,则设置正确的值。有任何想法吗??

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

4

2 回答 2

0

我认为问题来自于使用 int 作为原始变量,而不是 NSInteger,后者是一个完整的整数类。

为了解决这个问题,我的想法是分配 NSInteger 并使用它,但由于 NSInteger 不能那样工作,我建议使用 NSNumber ,因为它的行为完全像一个对象

 sharedSingleton.showCommentOrCreate = [NSNumber numberWithInt:1];
于 2013-01-24T16:51:02.443 回答
0

我太傻了。我很抱歉。我什至没有意识到在分配 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];

}

...再次对不起

于 2013-01-24T20:18:04.573 回答