您的位置:首页 > 移动开发 > IOS开发

学习IOS开发的第7天

2014-03-19 20:11 351 查看
今天学习了常用控件的使用。有UILabel、UITextField、UIButton、UISlider、UISegmentedControl、UIActivityIndicatorView、UIAlertView和imageView等。

我先写了一个视图控制器继承UIViewController,然后覆盖里面的loadView方法,创建一个视图。
UIView * view1 = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view1.backgroundColor = [UIColor whiteColor];
self.view =view1;
然后我开始添加控件。

UILabel是标签,用于显示一些文字。

//标签
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
label.text = @"你好";
label.tag = 001;
label.backgroundColor = [UIColor yellowColor];
[view1 addSubview:label];
UITextField是文本框,可以获取用户的输入。
//文本框
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 50)];
textField.backgroundColor = [UIColor whiteColor];
textField.placeholder = @"请输入内容";
[view1 addSubview:textField];
[textField addTarget:self action:@selector(inputFinish:)forControlEvents:UIControlEventEditingDidEndOnExit];
我给UITextField添加了一个事件,触发事件会执行下面的方法。
//输入完成方法
-(void)inputFinish:(UIButton *)button{
//失去响应
[button resignFirstResponder];
}
UIButton是按钮控件。
//按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 150, 100, 50);
[button setTitle:@"按钮" forState:UIControlStateNormal];
[button addTarget:self action:@selector(onTouch:) forControlEvents:UIControlEventTouchDown];
[view1 addSubview:button];
我也给按钮添加了一个事件,触发时会执行下面的方法。
//按下按钮
-(void)onTouch:(UIButton *)button{
button.backgroundColor = [UIColor blueColor];
}
UISlider是滑动条。
//滑动条
UISlider *slider =[[UISlider alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];
//设置初始值
slider.value =0.6;
[view1 addSubview:slider];


UISegmentedControl是分段控件。

//分段控件
UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:@[@"图片",@"视频",@"音乐"]];
sc.frame = CGRectMake(50, 250, 200, 50);
[view1 addSubview:sc];
UIActivityIndicatorView是活动指示器,用于告诉用户有操作正在执行。

//风火轮
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
aiv.frame = CGRectMake(50, 300, 50, 50);
//启动
[aiv startAnimating];
[view1 addSubview:aiv];
UIAlertView是提醒窗口,弹窗显示一些信息。

//提醒框
UIAlertView *alet = [[UIAlertView alloc] initWithTitle:@"提醒框" message:@"你好吗" delegate:nil cancelButtonTitle:@"不好" otherButtonTitles:@"好", nil];
[alet show];
imageView用于显示图片。

//图像视图
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.jpeg"]];
imageView.frame = CGRectMake(50, 350, 100, 100);
[view1 addSubview:imageView];
实现了上述控件,在应用程序的委托类的application: didFinishLaunchingWithOptions:方法里创建我之前写好的视图控制器,并显示。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

//创建视图控制器
UIViewController *viewController = [[MyViewController alloc] init];
self.window.rootViewController =viewController;

[self.window makeKeyAndVisible];
return YES;
}
运行结果截图:
提醒框



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: