iOS UI-UIPickerView(拾取器)、UIWebView(网页视图)和传值方式
2016-01-07 16:25
507 查看
// // 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(30, 120, 315, 50)]; self.lblDelegate.backgroundColor = [UIColor orangeColor]; self.lblDelegate.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.lblDelegate]; //创建跳转按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(30, 240, 315, 50)]; [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(30, 120, 315, 50)]; self.lblNotification.backgroundColor = [UIColor orangeColor]; self.lblNotification.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.lblNotification]; //创建跳转按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setFrame:CGRectMake(30, 240, 315, 50)]; [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(50, 300, 100, 100)]; //创建图片数组 NSMutableArray *imgArr = [NSMutableArray array]; for (int i = 0; i < 80; i++) { //获取图片名称 NSString *imgName = [NSString stringWithFormat:@"0%dda.png",i]; //根据图片名称获取图片 UIImage *image = [UIImage imageNamed:imgName]; //把图片加载到图片数组中 [imgArr addObject:image]; } //设置动画图片 [self.gifImgView setAnimationImages:imgArr]; //播放时间 [self.gifImgView setAnimationDuration:8]; //重复次数 [self.gifImgView setAnimationRepeatCount:0]; //开启动画 [self.gifImgView startAnimating]; [self.view addSubview:self.gifImgView]; } #pragma mark - 拾取器 - (void)createPickerView { self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 150, 355, 300)]; self.pickerView.dataSource = self; self.pickerView.delegate = self; [self.view addSubview:self.pickerView]; NSArray *arr1 = @[@"苹果",@"香蕉",@"橘子",@"葡萄",@"菩提"]; NSArray *arr2 = @[@"汽车",@"飞机",@"轮船",@"自行车",@"呵呵"]; NSArray *arr3 = @[@"11",@"22",@"33",@"44",@"55"]; self.pickerDataSource = [[NSMutableArray alloc] initWithObjects:arr1,arr2,arr3, nil]; //拾取器显示标签 self.lblShowPickerView = [[UILabel alloc] initWithFrame:CGRectMake(10, 500, 355, 80)]; [self.lblShowPickerView setBackgroundColor:[UIColor orangeColor]]; self.lblShowPickerView.textAlignment =NSTextAlignmentCenter; [self.view addSubview:self.lblShowPickerView]; //拾取器显示标签arr1 self.lblShowPickerView1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 115, 80)]; [self.lblShowPickerView1 setBackgroundColor:[UIColor orangeColor]]; self.lblShowPickerView1.textAlignment =NSTextAlignmentCenter; [self.lblShowPickerView addSubview:self.lblShowPickerView1]; //拾取器显示标签arr2 self.lblShowPickerView2 = [[UILabel alloc] initWithFrame:CGRectMake(115, 0, 115, 80)]; [self.lblShowPickerView2 setBackgroundColor:[UIColor orangeColor]]; self.lblShowPickerView2.textAlignment =NSTextAlignmentCenter; [self.lblShowPickerView addSubview:self.lblShowPickerView2]; //拾取器显示标签arr3 self.lblShowPickerView3 = [[UILabel alloc] initWithFrame:CGRectMake(230, 0, 115, 80)]; [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 80; } // 设置行高 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 50; } // 设置行内容 - (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 0: self.lblShowPickerView1.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; break; case 1: self.lblShowPickerView2.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; break; case 2: self.lblShowPickerView3.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; break; default: break; } } #pragma mark - 日期拾取器 - (void)createDatePicker { //日期拾取器(ps:拾取器的高度锁定不变) self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 200, 355, 300)]; //不管设置日期拾取器的日期格式如何,拾取器实际确定的时间都是完整的时刻 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(10, 500, 355, 80)]; [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(30, 240, 315, 50)]; [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(30, 240, 315, 50)]; [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 1.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; 2.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; 3.数据源方法(UIPickerViewDataSource) // 一共有多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // 第component列一共有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 4.代理方法(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 1.常见属性 // datePicker的显示模式 @property (nonatomic) UIDatePickerMode datePickerMode; // 显示的区域语言 @property (nonatomic, retain) NSLocale *locale; 2.监听UIDatePicker的选择 * 因为UIDatePicker继承自UIControl,所以通过addTarget:...监听 三.程序启动的完整过程 1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 3.delegate对象开始处理(监听)系统事件(没有storyboard) * 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法 * 在application:didFinishLaunchingWithOptions:中创建UIWindow * 创建和设置UIWindow的rootViewController * 显示窗口 3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard) * 创建UIWindow * 创建和设置UIWindow的rootViewController * 显示窗口
相关文章推荐
- Android开发艺术探索学习笔记(七)
- unity 3D 添加多个shader遮罩
- Android——获取手机屏幕的宽高
- Speeding up AngularJS apps with simple optimizations
- Android官方文档阅读之旅——Device Compatibility
- [Unity 3D教程]教你如何在3D场景中选择物体并显示轮廓效果
- IOS应用在iPhone5和iPhone5s上不能全屏显示,应用画面上下各有1条黑色的解决方案
- iOS 数据持久化的几种方式
- 动态适应label的高度
- unity3D中一些小功能的实现
- Android ComponentName的用法
- iOS 键盘遮挡问题
- ubuntu 安装 swift 64位
- Android studio测试使用
- Swift 中的委托/代理模式(转载)
- iOS中,如何做到未知参数数量的反射
- Swift 调用oc 桥接头文件
- 转载自简书iOS 异步绘制相关
- Swift中XMPP的简单使用
- Swift Strings and Characters