我使用nsnumber作为计数器,但它没有保留其值。它可能是一个指针问题,因为它在我的init语句中显示为13但在我的tap中显示为null。我不确定这是怎么回事。我从来没有真正搞过我的属性自动生成的_instanceValue。我一直使用self.propertyName作为访问者。我相信这可能与它有关。
头文件
@interface PezStoryViewer : UIViewController
@property(nonatomic, strong) NSNumber *counter;
-(id)initWithScene:(NSArray *)scenes;
+(void)viewTapped:(UIGestureRecognizer *)gesture;
+ (id)sharedInstance;
@end
m file
-(id)initWithScene:(NSArray *)scenes{
self = [super init];
if (self) {
//reverse array order
scenes = [[scenes reverseObjectEnumerator] allObjects];
//set counter
_counter = [[NSNumber alloc] initWithUnsignedInteger:[scenes count]];
NSLog(@"_counter = %@", _counter);
//initialize subviews
//and other stuff
}
return self;
}
-(void)viewTapped:(UIGestureRecognizer *)gesture{
NSLog(@"tap");
NSLog(@"_counter = %@", _counter);
NSUInteger i = [_counter unsignedIntegerValue];
i--;
NSLog(@"counter = %d", i);
if ([gesture.view isKindOfClass:[PezFrontImageView class]]) {
//3 Steps
//1: pop off subview
[gesture.view removeFromSuperview];
//2: if next view is a video, play it
//3: initialize new frontView under the existing backView
}
if (i==7) {
[self dismissViewControllerAnimated:NO completion:nil];
}
_counter = [NSNumber numberWithUnsignedInt:i];
}
+ (id)sharedInstance {
static id sharedInstance;
@synchronized(self) {
if (!sharedInstance)
sharedInstance = [[PezStoryViewer alloc] init];
return sharedInstance;
}
}
点击我的子视图中的UIImageView自定义类
类型的手势 //add tapGesture
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:[PezStoryViewer sharedInstance] action:@selector(viewTapped:)];
[self addGestureRecognizer:tapGesture];
答案 0 :(得分:1)
请注意,您的sharedInstance
方法不会致电initWithScene:
。它只是调用init
。所以你永远不会初始化counter
属性。
您真的不应该将视图控制器设置为单例。只需在需要时创建视图控制器并使用initWithScene:
方法。