视图介绍
- 视图时界面上的矩形构建块;
- 一个应用经常由若干个视图组合而成;
- 使用视图的目的:
- 展示应用的内容
- 导航视图
视图的分类
- view 大部分视图只是用来呈现,而内容不可以编辑
- controls 可以和用户进行交互,内容可以编辑
UIButton 简介
UIButton 时 iOS 中最常见的视图空间,常用来响应用户的点击事件。事件方法定义了要完成的功能。
使用IB实现
助手编辑器 Ctrl + 左键 拖拽生成
使用代码实现
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIButton *mybutton;
//代码创建
@property (strong, nonatomic) IBOutlet UIButton *mybutton2;
@end
@implementation ViewController
- (IBAction)click:(id)sender {
NSLog(@"click");
}
- (IBAction)click2:(UIButton *)sender forEvent:(UIEvent *)event {
NSLog(@"click2...");
}
- (IBAction)custom:(id)sender{
NSLog(@"custom...");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 代码实现 状态属性
self.mybutton2 = [UIButton buttonWithType:UIButtonTypeSystem];
self.mybutton2.frame = CGRectMake(30, 30, 100, 200);
[self.mybutton2 setTitle:@"Login..." forState:UIControlStateNormal];
// 添加按钮
[self.view addSubview:self.mybutton2];
// target - action 响应事件
[self.mybutton2 addTarget:self action:@selector(custom:) forControlEvents:UIControlEventTouchUpInside];
}
@end
UIActionSheet 简介
‘UIActionSheet’ is deprecated: first deprecated in iOS 8.3 - UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead
已经被废弃不在使用了,用UIAlertController代替!
UIActionSheet 和 UIAlertView 用法类似,区别在于 UIActionSheet 为用户提供了多个可选项,并且 UIActionSheet 需要指出它出现的位置。
创建 实现代理
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIActionSheetDelegate>
@end
- (IBAction)showActionSheet:(id)sender {
// UIActionSheet
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"Test ActionSheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"destruct" otherButtonTitles:@"Other ",@"Other2",@"Other3", nil];
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{
NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
NSLog(@"%@", title);
}
UIAlertView 简介
UIAlertView 视图为用户显示一个提示信息,例如:要求用户确认某个决定;从用户获得一些信息,例如:用户登录等。
创建 实现代理
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIAlertViewDelegate>
@end
- (IBAction)showAlert:(id)sender {
// NSLog(@"showAlert");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK1",@"OK2", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0){
switch(buttonIndex){
case 0:
NSLog(@"Cancel");
break;
case 1:
NSLog(@"OK1");
break;
case 2:
NSLog(@"OK2");
break;
default:
break;
}
}
自动以样式
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0, //默认样式
UIAlertViewStyleSecureTextInput, //密码输入框
UIAlertViewStylePlainTextInput, //平台文件输入框
UIAlertViewStyleLoginAndPasswordInput //登录框
} __TVOS_PROHIBITED;