Iphone 在iOS上,我们可以很容易地通过代码创建一个出口——那么操作呢?
在iOS上,如果我们使用Interface Builder,我们可以轻松创建Outlet和Action 如果我们使用Objective-C代码而不是Interface Builder,我们似乎也可以很容易地创建outlet,只需Iphone 在iOS上,我们可以很容易地通过代码创建一个出口——那么操作呢?,iphone,ios,uiviewcontroller,ibaction,Iphone,Ios,Uiviewcontroller,Ibaction,在iOS上,如果我们使用Interface Builder,我们可以轻松创建Outlet和Action 如果我们使用Objective-C代码而不是Interface Builder,我们似乎也可以很容易地创建outlet,只需 datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(200, 200, 200, 200)]; [self.view addSubview:datePicker]; 我们在.h文件中定义了一个实例变量
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
[self.view addSubview:datePicker];
我们在.h
文件中定义了一个实例变量
UIDatePicker *datePicker;
我觉得这就像一个出口
对于操作,我们如何纯粹使用Objective-C代码(不使用界面生成器)为不同类型的用户交互创建操作?使用
iAction
宏:
// in .h
-(IBAction) myAction:(id) sender;
使用
iAction
宏:
// in .h
-(IBAction) myAction:(id) sender;
就这样,
- (void)someMethod {
//...
[button_ addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//...
}
//...
// Either |IBAction| or |void| is okay,
// the former one is just used to be shown in Interface Builder
- (void)buttonAction:(id)sender {
// your action code here
}
注意:IBOutlet
和IBAction
仅适用于IB(Interface Builder的缩写)。如果您不想使用Interface Builder来管理视图和操作,可以将其忘记。就像这样:
- (void)someMethod {
//...
[button_ addTarget:self
action:@selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
//...
}
//...
// Either |IBAction| or |void| is okay,
// the former one is just used to be shown in Interface Builder
- (void)buttonAction:(id)sender {
// your action code here
}
注意:
IBOutlet
和IBAction
仅适用于IB(Interface Builder的缩写)。如果您不想使用Interface Builder管理您的视图和操作,您可以忘记它。我确信他指的是addTarget:action:forControlEvents:
来自UIControl
类,尽管他的问题确实很模糊。我的意思是,我们可以使用Interface Builder的outlet和action拥有事件处理程序并更改控件的属性。如果我们纯粹通过Objective-C(没有界面生成器)来实现它会怎么样?我确信他指的是来自UIControl
类的addTarget:action:forControlEvents:
,尽管他的问题确实很模糊。我的意思是,我们可以使用界面生成器的outlet和action来拥有事件处理程序并更改控件的属性。如果我们纯粹通过Objective-C(没有界面生成器)来实现,会怎么样?为什么您的按钮变量是button\uuu
(带下划线)?我还看到一些代码,例如ViewController将\u view
作为实例变量,但下划线在前面。他选择用下划线定义匹配的IVAR。这只是传统,事实上,@synthetic button=hrighaohfunaksifh\uuuuuuu
也同样有效@動靜能量 科达菲是对的。如果您对……感兴趣,可以参考它。:)因此,惯例是,如果是iOS框架代码,则使用\u foo
作为ivar名称,如果是我们自己的应用程序代码,则使用foo
作为ivar名称。。。这是为了区分IVAR和本地VAR。。。有点像Ruby对IVAR的@foo
@動靜能量 使用\u foo
很好,只是要小心命名。:)为什么按钮变量是按钮
(带下划线)?我还看到一些代码,例如ViewController将\u view
作为实例变量,但下划线在前面。他选择用下划线定义匹配的IVAR。这只是传统,事实上,@synthetic button=hrighaohfunaksifh\uuuuuuu
也同样有效@動靜能量 科达菲是对的。如果您对……感兴趣,可以参考它。:)因此,惯例是,如果是iOS框架代码,则使用\u foo
作为ivar名称,如果是我们自己的应用程序代码,则使用foo
作为ivar名称。。。这是为了区分IVAR和本地VAR。。。有点像Ruby对IVAR的@foo
@動靜能量 使用\u foo
很好,只是要小心命名。:)