iOS UI-UIPickerView(拾取器)、UIWebView(网页视图)和传值方式
//
// ViewController.m
// IOS_0107_finalToolClass
//
// Created by ma c on 16/1/7.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "NotificationViewController.h"
#import "DelegatePassValueViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,PassValueDelegate>
// 日期拾取器
@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UILabel *lblShowDate; // 拾取器
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) UILabel *lblShowPickerView;
@property (strong, nonatomic) UILabel *lblShowPickerView1;
@property (strong, nonatomic) UILabel *lblShowPickerView2;
@property (strong, nonatomic) UILabel *lblShowPickerView3;
@property (strong, nonatomic) NSMutableArray *pickerDataSource; //动态图
@property (strong, nonatomic) UIImageView *gifImgView; //通知传值
@property (strong, nonatomic) UILabel *lblNotification;
//代理传值
@property (strong, nonatomic) UILabel *lblDelegate; @end @implementation ViewController /*
1.日期拾取器:UIDatePicker
2.拾取器: UIPickerView
3.动态图:
4.通知传值:
5.代理传值:ps-Block(界面传值)
6.网页视图: UIWebView js + oc 混合开发
java + oc 混合开发
HTML + oc 混合开发 */ - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
//[self createDatePicker];
//[self createPickerView];
//[self createGifImageView];
//[self createNotificationByValue];
//[self createDelegatePassValue];
[self createWebView];
}
#pragma mark - 网页视图
- (void)createWebView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; //创建[全球支援定位器]
NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com"]; //创建网络请求
NSURLRequest *baiduRequest = [NSURLRequest requestWithURL:baiduURL]; //网页视图加载[网络请求]
[webView loadRequest:baiduRequest]; //添加网页视图
[self.view addSubview:webView];
} #pragma mark - 代理传值 - (void)createDelegatePassValue
{
//创建传递消息的Label
self.lblDelegate = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.lblDelegate.backgroundColor = [UIColor orangeColor];
self.lblDelegate.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.lblDelegate];
//创建跳转按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(, , , )];
[btn setTitle:@"gotoNextView" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor purpleColor]];
[btn addTarget:self action:@selector(detegatePassValue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; } - (void)detegatePassValue
{
DelegatePassValueViewController *delegateVC = [[DelegatePassValueViewController alloc] init];
//设置代理传值界面的delegate是当前的VC
delegateVC.delegate = self; [self presentViewController:delegateVC animated:YES completion:nil];
}
//代理传值的协议方法- 回调方法
- (void)my_passValue:(NSString *)str
{
self.lblDelegate.text = str;
} #pragma mark - 通知传值
- (void)createNotificationByValue
{
//创建传递消息的Label
self.lblNotification = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.lblNotification.backgroundColor = [UIColor orangeColor];
self.lblNotification.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.lblNotification];
//创建跳转按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(, , , )];
[btn setTitle:@"gotoNextView" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor purpleColor]];
[btn addTarget:self action:@selector(gotoNextView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; //想通知中心注册观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSecret:) name:@"passValue" object:nil]; }
// 通知回调
- (void)getSecret:(NSNotification *)notification
{
if ([notification.object isKindOfClass:[NSString class]]) {
self.lblNotification.text = notification.object;
}
}
//通知传值按钮关联方法
- (void)gotoNextView
{
NotificationViewController *notificationVC = [[NotificationViewController alloc] init];
[self presentViewController:notificationVC animated:YES completion:nil];
}
#pragma mark - gif动态图
- (void)createGifImageView
{
self.gifImgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
//创建图片数组
NSMutableArray *imgArr = [NSMutableArray array];
for (int i = ; i < ; i++) {
//获取图片名称
NSString *imgName = [NSString stringWithFormat:@"0%dda.png",i];
//根据图片名称获取图片
UIImage *image = [UIImage imageNamed:imgName];
//把图片加载到图片数组中
[imgArr addObject:image];
}
//设置动画图片
[self.gifImgView setAnimationImages:imgArr];
//播放时间
[self.gifImgView setAnimationDuration:];
//重复次数
[self.gifImgView setAnimationRepeatCount:];
//开启动画
[self.gifImgView startAnimating];
[self.view addSubview:self.gifImgView];
} #pragma mark - 拾取器
- (void)createPickerView
{
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , , )];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
[self.view addSubview:self.pickerView]; NSArray *arr1 = @[@"苹果",@"香蕉",@"橘子",@"葡萄",@"菩提"];
NSArray *arr2 = @[@"汽车",@"飞机",@"轮船",@"自行车",@"呵呵"];
NSArray *arr3 = @[@"",@"",@"",@"",@""]; self.pickerDataSource = [[NSMutableArray alloc] initWithObjects:arr1,arr2,arr3, nil];
//拾取器显示标签
self.lblShowPickerView = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self.lblShowPickerView setBackgroundColor:[UIColor orangeColor]];
self.lblShowPickerView.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:self.lblShowPickerView];
//拾取器显示标签arr1
self.lblShowPickerView1 = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self.lblShowPickerView1 setBackgroundColor:[UIColor orangeColor]];
self.lblShowPickerView1.textAlignment =NSTextAlignmentCenter;
[self.lblShowPickerView addSubview:self.lblShowPickerView1]; //拾取器显示标签arr2
self.lblShowPickerView2 = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self.lblShowPickerView2 setBackgroundColor:[UIColor orangeColor]];
self.lblShowPickerView2.textAlignment =NSTextAlignmentCenter;
[self.lblShowPickerView addSubview:self.lblShowPickerView2]; //拾取器显示标签arr3
self.lblShowPickerView3 = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self.lblShowPickerView3 setBackgroundColor:[UIColor orangeColor]];
self.lblShowPickerView3.textAlignment =NSTextAlignmentCenter;
[self.lblShowPickerView addSubview:self.lblShowPickerView3]; }
#pragma mark - 拾取器的数据源方法
// 创建分组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.pickerDataSource.count;
}
// 创建分行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [[self.pickerDataSource objectAtIndex:component] count]; }
#pragma mark - 拾取器的代理方法
// 设置组宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return ;
}
// 设置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return ;
}
// 设置行内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row];
}
// 选中内容后调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case :
self.lblShowPickerView1.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row];
break;
case :
self.lblShowPickerView2.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row];
break;
case :
self.lblShowPickerView3.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row];
break; default:
break;
}
} #pragma mark - 日期拾取器
- (void)createDatePicker
{
//日期拾取器(ps:拾取器的高度锁定不变)
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];
//不管设置日期拾取器的日期格式如何,拾取器实际确定的时间都是完整的时刻
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker setDate:[NSDate date]];
[self.datePicker addTarget:self action:@selector(dateChange) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.datePicker];
//日期显示标签
self.lblShowDate = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[self.lblShowDate setBackgroundColor:[UIColor orangeColor]];
self.lblShowDate.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.lblShowDate];
}
#pragma mark - 日期拾取器关联方法
- (void)dateChange
{
NSLog(@"%@",self.datePicker.date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:MM:ss"];
NSString *dateStr = [formatter stringFromDate:self.datePicker.date];
self.lblShowDate.text = dateStr; } #pragma mark - 状态栏 - (BOOL)prefersStatusBarHidden
{
return YES;
} @end
#import <UIKit/UIKit.h> @interface NotificationViewController : UIViewController @end #import "NotificationViewController.h" @interface NotificationViewController () @end @implementation NotificationViewController - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor cyanColor]]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(, , , )];
[btn setTitle:@"passValue" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor purpleColor]];
[btn addTarget:self action:@selector(passValue) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; }
- (void)passValue
{
//返回上一次模态视图
[self dismissViewControllerAnimated:YES completion:nil]; NSString *str = @"here is a secret"; //发送带消息的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"passValue" object:str];
} @end
#import <UIKit/UIKit.h> @protocol PassValueDelegate <NSObject> - (void)my_passValue:(NSString *)str; @end @interface DelegatePassValueViewController : UIViewController //实现协议的代理
@property(strong, nonatomic) id<PassValueDelegate> delegate; @end #import "DelegatePassValueViewController.h" @interface DelegatePassValueViewController () @end @implementation DelegatePassValueViewController - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor cyanColor]]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(, , , )];
[btn setTitle:@"passValueBack" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor purpleColor]];
[btn addTarget:self action:@selector(passValueBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
} - (void)passValueBack
{
[self dismissViewControllerAnimated:YES completion:nil]; NSString *str = @"another secret";
//让代理执行协议方法
if ([self.delegate respondsToSelector:@selector(my_passValue:)]) {
[self.delegate performSelector:@selector(my_passValue:) withObject:str];
}
} @end
笔记
一.UIPickerView
.UIPickerView的常见属性
// 数据源(用来告诉UIPickerView有多少列多少行)
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
@property(nonatomic,assign) id<UIPickerViewDelegate> delegate;
// 是否要显示选中的指示器
@property(nonatomic) BOOL showsSelectionIndicator;
// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents; .UIPickerView的常见方法
// 重新刷新所有列
- (void)reloadAllComponents;
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component; // 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component; .数据源方法(UIPickerViewDataSource)
// 一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; .代理方法(UIPickerViewDelegate)
// 第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; // 第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; // 第component列第row行显示怎样的view(内容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; // 选中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 二.UIDatePicker
.常见属性
// datePicker的显示模式
@property (nonatomic) UIDatePickerMode datePickerMode;
// 显示的区域语言
@property (nonatomic, retain) NSLocale *locale; .监听UIDatePicker的选择
* 因为UIDatePicker继承自UIControl,所以通过addTarget:...监听 三.程序启动的完整过程
.main函数 .UIApplicationMain
* 创建UIApplication对象
* 创建UIApplication的delegate对象 .delegate对象开始处理(监听)系统事件(没有storyboard)
* 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法
* 在application:didFinishLaunchingWithOptions:中创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口 .根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)
* 创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口
iOS UI-UIPickerView(拾取器)、UIWebView(网页视图)和传值方式的更多相关文章
- [Swift通天遁地]一、超级工具-(5)使用UIWebView(网页视图)加载本地页面并调用JavaScript(脚本)代码
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]一、超级工具-(4)使用UIWebView(网页视图)加载HTML和Gif动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ios学习之UIWebView网页视图
转载于爱德凡的百度空间,地址:http://hi.baidu.com/aidfan/item/34a720866b33cbcdef083d37 UIWebView 使用详解 一.UIWebView加载 ...
- iOS 浅谈MVC设计模式及Controllers之间的传值方式
1.简述你对MVC的理解? MVC是一种架构设计.它考虑了三种对象:Model(模型对象).View(试图对象).Controller(试图控制器) (1)模型:负责存储.定义.操作数据 (2)视图: ...
- ios学习之UIWebView网页视图调整
//先来一个可行的小Demo程序:结合searchBar的google搜索 #import <UIKit/UIKit.h> @interface ViewController : UIVi ...
- iOS UI基础-15.0 UIWebView
WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...
- Django内置auth模块中login_required装饰器用于类视图的优雅方式
使用多继承 以及类似java中的静态代理模式 原理:OrderView.as_view()根据广度优先,调用的是LoginRequiredMixin中的as_view(cls, *args, **kw ...
- Asp.Net Core MVC控制器和视图之间传值
一.Core MVC中控制器和视图之间传值方式和Asp.Net中非常类似 1.弱类型数据:ViewData,ViewBag 2.强类型数据:@model 二.代码 实例 1.ViewData pub ...
- iOS:网页视图控件UIWebView的详解
网页视图控件:UIWebView 功能:它是继承于UIView的,是一个内置的浏览器控件,以用来浏览从网络下载下来的网页或者本地上加载下来的文档. 枚举: //网页视图导航类型 typedef NS_ ...
随机推荐
- P4009 汽车加油行驶问题
P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...
- 在Android Studio中创建项目和模拟器
北京电子科技学院 实 验 报 告 课程:移动平台应用开发实践 班级:201592 姓名:杨凤 学号:20159213 成绩:___________ 指导老师:娄嘉 ...
- CAN/J1850/
(1)CAN:(差分信号)有信号CANH=3.5V,CANL=1.5V, 没有信号CANH=2.5V,CANL=2.5V 速率:CAN系统又分为高速和低速,高速CAN系统采用硬线是动力型,速度:500 ...
- codevs 1423 骑士 - Tarjan - 动态规划
题目描述 Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵 ...
- Windows Services 学习(转载)
转载:http://blog.csdn.net/fakine/article/details/42107571 一.学习点滴 1.本机服务查看:services.msc /s2.服务手动安装(使用sc ...
- TCP客户端【TcpClient】
一.阻塞模式 1.命名空间 System.Net.Sockets 2.对象声明 TcpClient dpu1TcpClient = null;//dpu1tcp客户端,TcpClient模式 Netw ...
- Hadoop新增和删除节点
#新增节点 1.安装lunix,和以前一样的版本 2.初始化系统环境 2.1.设置静态ip vi /etc/sysconfig/network-scripts/ifcfg-eth0 //增加 #Adv ...
- 使用CLR Profiler查看C#运行程序的内存占用情况
http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...
- 在线js编程网站 精品版
https://jsfiddle.net/ 引自:http://www.ykmimi.com/tools.html
- layer.alert自定义关闭回调事件
在项目应用中,遇到自定义关闭layer.alert弹出层,即在关闭layer.alert时,可以自动触发关闭时的事件, 具体方法为: layer.alert('爱心提示!', function(){ ...