//
// 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
* 显示窗口