首页 > Web开发 > 详细

常用控件补充(UIDatePicker、UIWebView)

时间:2016-01-23 15:30:10      阅读:248      评论:0      收藏:0      [点我收藏+]

一、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];

 }

 

常用控件补充(UIDatePicker、UIWebView)

原文:http://www.cnblogs.com/HOYF/p/5153382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!