2013-03-24 79 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; 
正確的腳