UITextField,在ViewController中定义它的位置

时间:2014-04-16 02:43:06

标签: ios objective-c uiviewcontroller uitextfield viewcontroller

我有几个代码示例。在@interface ViewController {}中定义两个文本字段(A和B)并在其外定义两个文本字段之间有什么区别?感谢。

http://pastie.org/9083686

http://pastie.org/9083687

2 个答案:

答案 0 :(得分:0)

内部类ViewController可以访问

@interface ViewController : UIViewController{
   UITextField *a; 
}

外部类ViewController可以访问

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *a;
@end

答案 1 :(得分:0)

@interface ViewController : UIViewController {
    UITextField *a;
}

@property (nonatomic, strong) UITextField *b;

a b 都是iVars,但 a 默认情况下没有隐式创建的setter / getter。
您可以通过执行类似

的操作来访问 a
if (a.text.length == 0) { ... }

我们称之为直接访问。

但是对于 b ,我们使用 self。来访问它。

if (self.b.text.length == 0) { ... }

通过使用 self。,您声明要使用setter / getter方法访问 b

您甚至可以通过直接访问 _b (如果您没有明确合成它)来选择不使用setter / getter,如下所示:

if (_b.text.length == 0) { ... }