2013-03-24 122 views
0

我有这样的情况:更新视图

Two View Controller and One View

如果我点击左脚按钮,我想,我在第二控制器视图显示左脚形象,如果我点击右键,视图应显示右脚图像。

在第一控制器我写这个选择左或右:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    RSViewController *vc = [segue destinationViewController]; 

    if ([[segue identifier] isEqualToString:@"Left-Foot"]) 
     [vc setLeft:YES]; 
    else 
     [vc setLeft:NO]; 
} 

在第二控制器我有设置一个布尔值

- (void)setLeft:(BOOL)left 
{ 
    if (left) { 
     _left = left; 
     NSLog(@"LEFT UPDATED IN RSVIEWCONTROLLER"); 
    }else { 
     _left = left; 
     NSLog(@"RIGHT UPDATED IN RSVIEWCONTROLLER"); 
    } 
} 

和在同一个控制器我的方法用我的一个init在viewdidload中创建我的视图:

- (void)viewDidLoad 
{ 
    NSLog(@"meeeea"); 
    [super viewDidLoad]; 
    CGRect rect = CGRectMake(0, 0, 100, 100); 
    self.animatedCircleView = [[RSAnimatedCircleView alloc] initWithFrameFoot:rect foot:YES]; 
    NSLog(@"Viewdidnoload"); 
} 

在我看来(小蓝色的矩形e在图中):

- (void)setup:(BOOL)left 
{ 
self.leftFoot = left; 
} 

- (void)setLeftFoot:(BOOL)leftFoot 
{ 
    _leftFoot = leftFoot; 
} 

- (void)awakeFromNib 
{ 
    [self setup:_leftFoot]; 
} 

- (id)initWithFrameFoot:(CGRect)frame foot:(BOOL)left 
{ 
    self = [super initWithFrame:frame]; 
    [self setup:left]; 
    self.leftFoot = left; 

    if (self.leftFoot) 
     NSLog(@"Left == TRUE"); 

    return self; 

} 

- (RSFootView *)footView 
{ 
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 
    if (self.leftFoot) { 
     NSLog(@"Now left is yes"); 

    }else { 
     NSLog(@"left is no"); 
    } 

    _footView = [[RSFootView alloc] initWithFrame:rect withLeftFoot:self.leftFoot]; 
    [_footView setBackgroundColor:[UIColor clearColor]]; 


    return _footView; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    if (self.leftFoot) 
     NSLog(@"left ada"); 
    else 
     NSLog(@"right masd"); 

    CGFloat x = self.bounds.size.height; 
    CGFloat y = self.bounds.size.width; 
    CGFloat radius = x/2 - 15; 
    CGPoint center = CGPointMake(x/2, y/2); 
    CGFloat startAngle = DEGREES_TO_RADIANS(START_ANGLE); 
    CGFloat endAngle = DEGREES_TO_RADIANS(END_ANGLE); 

    // Drawing the sector 
    UIBezierPath *sector = [UIBezierPath bezierPathWithArcCenter:center 
                  radius:radius 
                 startAngle:startAngle 
                 endAngle:endAngle 
                 clockwise:YES]; 

    sector.lineWidth = 4; 

    [[UIColor grayColor] setStroke]; 
    [sector stroke]; 
    [self setBackgroundColor:[UIColor clearColor]]; 
    [sector addClip]; 

    [self addSubview:self.footView]; 

} 

在方法“drawRect”中,leftfoot值始终为false。我不知道为什么是这样的...任何人都可以帮助我吗?

谢谢

回答

2

你设置你的观点,以“左”,在下面的行各一次。

self.animatedCircleView = [[RSAnimatedCircleView alloc] 
      initWithFrameFoot:rect foot:YES]; 

这是变得非常有可能不给你的变量和理解的方法和直观的名称时,一个典型的错误。如果你不修补你的方式,预计这种类型的错误会频繁发生。

我会给你的观点,即国家明确它是什么山脚下的枚举属性:

typedef enum {LeftFoot, RightFoot} FootType; 
@property (strong, nonatomic) FootType footType; 

另外,你应该修改你的方法名。我会完全消除initWithFrameFoot方法,并在initWithFrame的覆盖范围内设置默认脚。 (将它留在0等于LeftFoot。)与@synthesize你也不需要setter或getter。删除它们。

如果你想要一个专用的初始化器,确保所传递的变量实际上是在正确的单词前面。

-(id) initWithFrame:(CGRect)rect andFootType:(FootType)footType { 
    self = [super initWithFrame:rect]; 
    if (self) { 
     _footType = footType; 
    } 
    return self; 
} 

使用@synthesize后,您可以设置prepareForSegue:

footViewController.footType = rightFoot; 
正确的脚