标签:ios uibutton ios开发 ios教程 ui
前面的配置&创建都搞好了的话,那么就开始应用开发的第一步:了解应用组件。比如说,按钮,文本框等等……这篇先介绍按钮。
1、创建按钮
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController *viewController = [UIViewController alloc];
self.window.rootViewController = viewController;
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController.view = rootView;
/**
* 创建 UIButton
*/
//创建按钮的时候,必须用 buttonWithType: 来创建,想上面的 UIView 或其他的可以用 alloc
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置按钮的位置、宽高。参数:X轴,Y轴,宽度,高度
button.frame = CGRectMake(100, 100, 100, 30);
//设置按钮的背景颜色
button.backgroundColor = [UIColor yellowColor];
//必须用这种方式来设置按钮的文字,同时设置它的状态。不能用button.titleLabel来设置文字,不显示
[button setTitle:@"我是按钮" forState:UIControlStateNormal];
//添加按钮到 view 控件显示
[rootView addSubview:button];
[self.window makeKeyAndVisible];
return YES;
}
1)UIButtonType 有如下几个枚举值:
UIButtonTypeCustom-----自定义
UIButtonTypeSystem-----标准系统默认
UIButtonTypeDetailDisclosure-----显示当前列表详情,显示 i
UIButtonTypeInfoLight-----显示简短信息,显示 i
UIButtonTypeInfoDark-----显示简短信息,显示 i
UIButtonTypeContactAdd-----添加信息,显示 +
2)UIControlState 有如下几个枚举值:
UIControlStateNormal-----默认
UIControlStateHighlighted-----高亮
UIControlStateDisabled-----禁用
UIControlStateSelected-----选中
按钮的其他属性,只能是靠去查看文档了解,一些零零碎碎的我就不写出来了。
运行结果:
2、绑定一个事件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
//使用 addTarget:action:forControlEvents: 方法就可以绑定一个事件
[button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
...
}
//绑定要实现的方法 Click:
- (void) Click:(id) sender
{
NSLog(@"我是一个按钮");
}运行后点击按钮的效果:
1)UIControlEvent 有如下枚举值:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
UIControlEventValueChanged
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllTouchEvents
UIControlEventAllEditingEvents
UIControlEventApplicationReserved
UIControlEventSystemReserved (application use)
UIControlEventAllEvents (framework use)
基础部分,简单介绍一下就行,以后碰到相关的再接着说明其他的使用方法。
第三章,结束!
本篇内容属原创,转载请注明出处,禁止用于商业用途。谢谢!
标签:ios uibutton ios开发 ios教程 ui
原文地址:http://blog.csdn.net/zsfz_053/article/details/41803045