2015-10-15 19 views
0

我在字典中的值,這將使一個UIButton設置的UIButton幀不工作

tags =   (
        { 
      height = "15.513672"; 
      text = jeans; 
      width = "39.808105"; 
      x = "225.500000"; 
      y = "265.000000"; 
     }, 
        { 
      height = "15.513672"; 
      text = jacket; 
      width = "44.080078"; 
      x = "190.000000"; 
      y = "156.500000"; 
     } 
    ); 

我設置uibuttons的框架上,讓值從字典

for (int i = 0; i<[tags count]; i++) { 
    NSLog(@"adding tags"); 

    NSMutableDictionary *propertiesdict = [[NSMutableDictionary alloc] init]; 
    propertiesdict = [[tags objectAtIndex:i] mutableCopy]; 

    float x = [[dict objectForKey:@"x"] floatValue]; 
    float y = [[dict objectForKey:@"y"] floatValue]; 
    float width = [[dict objectForKey:@"width"] floatValue]; 
    float height = [[dict objectForKey:@"height"] floatValue]; 

    NSString *title = [dict objectForKey:@"text"]; 

    UIButton *button = [[UIButton alloc] init]; 
    [button setFrame:CGRectMake(x, y, width, height)]; 
    [button setTitle:title forState:UIControlStateNormal]; 
    button.layer.cornerRadius = 2.5; 
    //button.alpha = 0.9; 
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]]; 
    [button setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; 
    button.tag = i; 
    [button addTarget:self action:@selector(tappedOnTag:) forControlEvents:UIControlEventTouchUpInside]; 

    [tagsView addSubview:button]; 
} 

加入後的框架按鈕到子視圖,NSLog顯示每個按鈕都有一個框架(0,0,0,0),它與我在for循環中設置框架時不同。

2015-10-15 12:25:17.787 kyss[1018:316548] tapped on tags view. Number of subviews = 2. Subviews: (
    "<UIButton: 0x15f732330; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x15f72eb60>>", 
    "<UIButton: 0x15f6d10e0; frame = (0 0; 0 0); opaque = NO; tag = 1; layer = <CALayer: 0x15f6b3790>>" 
) 
+0

循環內部的'x','y','width'和'height'的值是否正確? – rmaddy

+0

@rmaddy是的,NSLog顯示2015-10-15 12:52:22.447 kyss [1044:324386]添加標籤 2015-10-15 12:52:22.447 kyss [1044:324386]屬性字典= { height = 「15.513672」; text = jacket; width =「44.080078」; x =「190.000000」; y =「156.500000」; } – dwizzylion

+1

這不能回答我的問題。實際的'x','y','width'和'height'變量是否正確?你似乎正在記錄'properiesDict'(它是從'tags'創建的),但你從'dict'得到了值。 – rmaddy

回答

1

讓我們來看看代碼...

for (int i = 0; i<[tags count]; i++) { 
    NSLog(@"adding tags"); 

    NSMutableDictionary *propertiesdict = [[NSMutableDictionary alloc] init]; 
    propertiesdict = [[tags objectAtIndex:i] mutableCopy]; 

您創建一個新的字典,並立即覆蓋從標籤陣列的項目的參考。

float x = [[dict objectForKey:@"x"] floatValue]; 
    float y = [[dict objectForKey:@"y"] floatValue]; 
    float width = [[dict objectForKey:@"width"] floatValue]; 
    float height = [[dict objectForKey:@"height"] floatValue]; 

您可以參考dict,這在您發佈的代碼中沒有定義。可以肯定的是,它與propertiesdict不一樣,所以你得到nil引用,因此每個賦值的值爲0。

NSString *title = [dict objectForKey:@"text"]; 

再次與dict

UIButton *button = [[UIButton alloc] init]; 
    [button setFrame:CGRectMake(x, y, width, height)]; 
    [button setTitle:title forState:UIControlStateNormal]; 
    button.layer.cornerRadius = 2.5; 
    //button.alpha = 0.9; 
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]]; 
    [button setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; 
    button.tag = i; 
    [button addTarget:self action:@selector(tappedOnTag:) forControlEvents:UIControlEventTouchUpInside]; 

    [tagsView addSubview:button]; 
} 

其餘的代碼是好的。

無論如何,你的問題是引用錯誤的字典。你沒有注意到標題也是錯的嗎?

+0

當按鈕的框架是0,0,0,0時,你怎麼能注意到錯誤的標題? – rmaddy

+0

他只能通過檢查調試器來告訴幀全部爲零。如果他檢查它,同樣的調試器會給他標題。 – Avi