如何在iPhone的UIbutton中限制属性访问?

3

我希望限制组件现有类的某些属性。

以下是示例代码:

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];
但是我希望限制对按钮属性的访问。例如,我只想访问property background color和alpha value,而不想访问其余属性。
请帮助我解决这个问题。
谢谢!

一种方法是通过子类化。 - Anoop Vaidya
简单地将属性翻译为中文即可,不需要为所有其他属性编写代码...嘿嘿嘿嘿嘿... :P - Rajneesh071
你不应该依赖于任何方法是不可调用的。方法都是公共的,你可以将它们放在类扩展或私有头文件中,这样当尝试在代码中调用它们时会得到警告...但在运行时仍然可以调用...你对实例变量的作用域有稍微更多的控制...但通常你不希望其他类访问你的实例变量(出于封装性考虑)。 - Grady Player
4个回答

1
在Objective-C中,所有的方法都是公共的,属性只不过是setter和getter(方法)而已。 因此你无法限制现有的方法/属性。 即使你去子类化,也只能重写它。 不过你可以添加一个NSAssert来确保任何人使用该方法时都会受到警告。

2
但是你可以在类扩展中构建私有属性... @interface MyClass () - Grady Player

0

看起来你需要一个自定义组件并覆盖所需的getter方法。我不知道其他实现方式,也许其他人会给你建议。

因此,这将不是简单的@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)ivar 中应该有一些限制。


如果您相信什么,该怎么办? - user529543

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

这是 Objective-C 相对于 C++ 缺失的一点,即继承模式中的 private、protected 和 public。

如果有了这些特性,你的工作就完成了,但事实并非如此。

在 Objective-C 中,没有私有或公共。每个东西都是公共的。你可以虚拟地不在类接口中显示方法,但仍然可以被知道其存在的人访问。从另一个类继承的类是所有“超级”类方法/属性和它自己的方法/属性的总和(在 Objective-C 中,只能使用公共模式从一个类派生一个类)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接