在我的 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];
}}}
如您所见,我将整数的值设置了两次,它有效,否则无效。如果没有,如果我以时间间隔调用代码两次,则设置正确的值。有任何想法吗??
编辑:更新了我声称可以工作的代码