2012-12-27 22 views
3

我想限制現有組件的某些屬性。如何限制iPhone中UIbutton屬性的訪問?

下面是示例代碼,

UIButton *customBtn; 
property (nonatomic, strong) UIButton *customBtn; 
@synthesize customBtn; 

這是我實現,

customBtn= [UIButton buttonWithType:UIButtonTypeCustom]; 
customBtn= setImage:[UIImage imageNamed"radio_button_noraml.png"] forState:UIControlStateNormal]; 
customBtn= setImage:[UIImage imageNamed"radio_button_acitve.png"] forState:UIControlStateSelected]; 
customBtn = CGRectMake(5, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)]; 
[customBtn addTargetelf actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:customBtn]; 

在我的視圖控制器,我可以使用customBtn訪問的UIButton的所有財產。

myClass.customBtn.backGroundcolor = [UIColor blackColor];

但我想限制按鈕屬性的訪問,例如我想要訪問propertybackground顏色和alpha值,我不想訪問的屬性的其餘部分。

所以請幫助我。

謝謝!

+0

在途中可能是子類。 –

+0

簡單...不寫代碼的所有其他財產.... hihihihihihih ...:P – Rajneesh071

+0

你不應該依靠任何方法是不可調用的。方法都是公開的,你可以做的就是把它們放在類擴展或私有頭文件中,這樣當你用代碼調用它們時會得到一個警告......但是在運行時它仍然是遊戲......你有用ivars稍微控制範圍...但是你通常不希望其他類達到你的ivars(爲了封裝起見) –

回答

0

不聲明控制器頭文件中的customBtn屬性,而是僅爲要公開公開的那些按鈕屬性聲明屬性。例如,在MyController.h:您使用在原始類的屬性聲明相同屬性的屬性

@interface MyController : UIViewController 
{ 
} 

@property(nonatomic, copy) UIColor* customBtnBackgroundColor; 

@end 

留意。在該示例中,按鈕的backgroundColor屬性實際上是由UIView(而不是UIButton)聲明的,所以如果您查看UIView.h,您會看到該屬性聲明的屬性爲nonatomiccopy

接下來,您需要在控制器的實現文件中私下聲明customBtn屬性。您還需要爲頭文件中公開的屬性的getter/setter方法提供實現。 getter/setter方法只是將調用轉發給實際的按鈕對象。例如,在MyController.m

#import "MyController.h" 

// Class extension with private methods and properties for MyController 
@interface MyController() 
    property (nonatomic, strong) UIButton* customBtn; 
@end 


@implementation MyController 

    @synthesize customBtn; 

    // Note: No need to synthesize customBtnBackgroundColor here because 
    // you don't need an instance variable, and you are providing the 
    // getter/setter implementation yourself. 


    - (UIColor*) customBtnBackgroundColor 
    { 
    return self.customBtn.backgroundColor; 
    } 

    - (void) setCustomBtnBackgroundColor:(UIColor*)backgroundColor 
    { 
    self.customBtn.backgroundColor = backgroundColor; 
    } 

@end 

正如其他人寫的,如果有人的MyController外界知道屬性customBtn存在,物業仍然可以訪問。但是,關鍵點應該是定義MyController的公共接口,並使用上面列出的解決方案完成儘可能多的預期。

0

看來你需要一個自定義組件並覆蓋所需的獲取者。我不知道其他方式實現,也許別人會給你。

所以它不會是一個簡單的@synthesize。

的@synthesize等效代碼:

// .h 
@property (nonatomic, retain) id ivar; 

// .m 
- (id)ivar { 
    return ivar; 
} 

- (void)setIvar:(id)newValue { 
    if (ivar != newValue) { // this check is mandatory 
     [ivar release]; 
     ivar = [newValue retain]; 
    } 
} 
現在在

- (ID),伊娃應該是有一些限制。

+1

沒有答案:( –

+0

如果你相信那怎麼辦? – 2012-12-27 13:05:40

+0

現在它看起來不錯 –

1

在objective-c中,所有的方法都是公共的,屬性只是setter和getter(方法)。
所以你不能限制現有的方法/屬性。
即使您繼承它,您也只能覆蓋它。
如果有人使用該方法,您可以在其中添加NSAssert

+0

這是我看到的最接近的一個很好的答案... –

+2

但你可以在類擴展中構建私有屬性......'@interface MyClass()' –

0

這是obj-c中缺少的東西之一,因爲我將它與C++進行了比較,繼承模式爲private,protected和public。

如果這些功能在這裏您的工作已完成,但事實並非如此。

在目標C中沒有私人或公共的。每件事都是公開的。你幾乎不能在類接口中顯示這些方法,但對於知道它存在的人來說仍然可以訪問它。從另一個類繼承的類是所有「超級」類方法/屬性以及它自己的方法/屬性的總和(在Objective-C中,只能從一個類派生,使用公共模式)。