常用控件补充(UIDatePickerUIWebView)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用控件补充(UIDatePickerUIWebView)相关的知识,希望对你有一定的参考价值。
一、UIDatePicker日期拾取器对象
- (void)viewDidLoad {
[super viewDidLoad];
//初始化日期拾取器对象
UIDatePicker * datePicker = [[UIDatePicker alloc] init];
//设置日期拾取器对象的中心位置
[datePicker setCenter:CGPointMake(200, 200)];
//设置日期拾取器的tag值,以便再次使用
[datePicker setTag:1];
[self.view addSubview:datePicker];
//添加一个按钮,当点击按钮时获取日期拾取器对象的值
CGRect rect = CGRectMake(110, 350, 100, 30);
UIButton * button = [[UIButton alloc] initWithFrame:rect];
[button setTitle:@"select" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor purpleColor]];
[button addTarget:self action:@selector(getDate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)getDate{
//通过tag获取日期拾取器对象
UIDatePicker * datePicker = (UIDatePicker *)[self.view viewWithTag:1];
//获取日期拾取器的日期值
NSDate * select = [datePicker date];
//新建一个日期格式化对象,用来实现日期的格式化
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
//设置日期的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//将日期转换为字符串
NSString * dateAndTime = [dateFormatter stringFromDate:select];
//使用警告弹出窗口,显示日期结果
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"时间" message:dateAndTime preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
效果:
二、UIWebView
- (void)viewDidLoad {
[super viewDidLoad];
// [self loadWeb];
[self loadhtml];
}
//1.UIWebView加载网页
- (void)loadWeb{
CGRect rect = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
UIWebView * webView = [[UIWebView alloc] initWithFrame:rect];
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
//创建一个网址请求对象,作为网页视图对象的网络请求
NSURLRequest * requestobj = [NSURLRequest requestWithURL:url];
//使用网页视图对象,加载设置的网址
[webView loadRequest:requestobj];
[self.view addSubview:webView];
}
//2.UIWebViw加载本地HTML代码
- (void)loadHTML{
CGRect rect = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);
UIWebView * webView = [[UIWebView alloc] initWithFrame:rect];
//新建一个HTML字符串
NSString * htmlString = @"<font color=‘red‘>Hello</font> <b>ios</b> <i>programe</i>";
[webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:webView];
}
以上是关于常用控件补充(UIDatePickerUIWebView)的主要内容,如果未能解决你的问题,请参考以下文章