2011-04-04 12 views
0

通過幀我在settings.plist定義的框架和在我的課accesing它作爲一個字符串,而通過它給出了一個錯誤:問題從setting.plist

下面的代碼:

NSString * frame = [selectable objectForKey:@"Frame"]; 
[self selectButtons:@"Select." andFrame:frame andValue:goal]; 


-(void)selectButtons:(NSString *)text andFrame:(NSString *)frame andValue:(int)goal 
{ 

    UIColor * c = [UIColor clearColor]; 
    UIFont * font = BUTTON_TITLE; 
    UIColor * f = DESC_Color_White; 
    select = [ApplicationHelpers buttonWithTitle:text andBgColor:c andTitleColor:f andTitleFont:font]; 
    CGRect frame1 = frame; -------->>>>>> gives error here 
    select.frame = frame1; 

    UIImage * buttonImage = [UIImage imageNamed:@"scneariosButtonSmall1.png"]; 
    UIImage * strechableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    [select setBackgroundImage:strechableButtonImage forState:UIControlStateNormal]; 
    [select addTarget:self action:@selector(selectClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:select]; 

} 

請給我一些關於這個的提示?

謝謝,

回答

2

一個NSString不能在CGRect之間隱式轉換。您需要使用NSStringFromCGRectCGRectFromString等功能來完成轉換。

CGRect frame1 = CGRectFromString(frame); 

... 

// when you need to save the CGRect: 
NSString* frameString = NSStringFromCGRect(frame1); 
+0

這工作,但唯一的是我通過我的框架作爲CGRectMake(20,30,150,54)和調試時,我檢查它沒有給任何價值。我的意思是x,y,寬度和高度都是零。所以我需要以不同的方式在我的設置文件中定義框架。 – Ashutosh 2011-04-04 18:42:33

+0

@Ashu:你的意思是字符串'frame'是'@「CGRectMake(20,30,150,54)」'? – kennytm 2011-04-04 18:44:04

+0

是的.....我發現它應該像這樣定義:{{x,y},{w,h}} – Ashutosh 2011-04-04 18:50:32

1

您不能只是將一個字符串分配給CGRect變量,並期望它可以工作。你必須將一種類型轉換爲另一種類型。根據您的幀字符串的格式,您可以使用CGRectFromString()函數(使用NSStringFromCGRect())從CGRect創建相應的字符串。或者你必須自己手動解析字符串。

或者,甚至可能更好,將您的幀值包含在NSValue中,其中+[NSValue valueWithCGRect:]-[NSValue CGRectValue]