1 // 2 // ViewController.m 3 // IOS_0107_finalToolClass 4 // 5 // Created by ma c on 16/1/7. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "NotificationViewController.h" 11 #import "DelegatePassValueViewController.h" 12 13 @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,PassValueDelegate> 14 // 日期拾取器 15 @property (strong, nonatomic) UIDatePicker *datePicker; 16 @property (strong, nonatomic) UILabel *lblShowDate; 17 18 // 拾取器 19 @property (strong, nonatomic) UIPickerView *pickerView; 20 @property (strong, nonatomic) UILabel *lblShowPickerView; 21 @property (strong, nonatomic) UILabel *lblShowPickerView1; 22 @property (strong, nonatomic) UILabel *lblShowPickerView2; 23 @property (strong, nonatomic) UILabel *lblShowPickerView3; 24 @property (strong, nonatomic) NSMutableArray *pickerDataSource; 25 26 //动态图 27 @property (strong, nonatomic) UIImageView *gifImgView; 28 29 //通知传值 30 @property (strong, nonatomic) UILabel *lblNotification; 31 //代理传值 32 @property (strong, nonatomic) UILabel *lblDelegate; 33 34 35 36 @end 37 38 @implementation ViewController 39 40 /* 41 1.日期拾取器:UIDatePicker 42 2.拾取器: UIPickerView 43 3.动态图: 44 4.通知传值: 45 5.代理传值:ps-Block(界面传值) 46 6.网页视图: UIWebView 47 48 js + oc 混合开发 49 java + oc 混合开发 50 HTML + oc 混合开发 51 52 */ 53 54 - (void)viewDidLoad { 55 [super viewDidLoad]; 56 [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; 57 //[self createDatePicker]; 58 //[self createPickerView]; 59 //[self createGifImageView]; 60 //[self createNotificationByValue]; 61 //[self createDelegatePassValue]; 62 [self createWebView]; 63 } 64 #pragma mark - 网页视图 65 - (void)createWebView 66 { 67 UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 68 69 //创建[全球支援定位器] 70 NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com"]; 71 72 //创建网络请求 73 NSURLRequest *baiduRequest = [NSURLRequest requestWithURL:baiduURL]; 74 75 //网页视图加载[网络请求] 76 [webView loadRequest:baiduRequest]; 77 78 //添加网页视图 79 [self.view addSubview:webView]; 80 } 81 82 #pragma mark - 代理传值 83 84 - (void)createDelegatePassValue 85 { 86 //创建传递消息的Label 87 self.lblDelegate = [[UILabel alloc] initWithFrame:CGRectMake(30, 120, 315, 50)]; 88 self.lblDelegate.backgroundColor = [UIColor orangeColor]; 89 self.lblDelegate.textAlignment = NSTextAlignmentCenter; 90 [self.view addSubview:self.lblDelegate]; 91 //创建跳转按钮 92 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 93 [btn setFrame:CGRectMake(30, 240, 315, 50)]; 94 [btn setTitle:@"gotoNextView" forState:UIControlStateNormal]; 95 [btn setBackgroundColor:[UIColor purpleColor]]; 96 [btn addTarget:self action:@selector(detegatePassValue) forControlEvents:UIControlEventTouchUpInside]; 97 [self.view addSubview:btn]; 98 99 } 100 101 - (void)detegatePassValue 102 { 103 DelegatePassValueViewController *delegateVC = [[DelegatePassValueViewController alloc] init]; 104 //设置代理传值界面的delegate是当前的VC 105 delegateVC.delegate = self; 106 107 [self presentViewController:delegateVC animated:YES completion:nil]; 108 } 109 //代理传值的协议方法- 回调方法 110 - (void)my_passValue:(NSString *)str 111 { 112 self.lblDelegate.text = str; 113 } 114 115 116 #pragma mark - 通知传值 117 - (void)createNotificationByValue 118 { 119 //创建传递消息的Label 120 self.lblNotification = [[UILabel alloc] initWithFrame:CGRectMake(30, 120, 315, 50)]; 121 self.lblNotification.backgroundColor = [UIColor orangeColor]; 122 self.lblNotification.textAlignment = NSTextAlignmentCenter; 123 [self.view addSubview:self.lblNotification]; 124 //创建跳转按钮 125 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 126 [btn setFrame:CGRectMake(30, 240, 315, 50)]; 127 [btn setTitle:@"gotoNextView" forState:UIControlStateNormal]; 128 [btn setBackgroundColor:[UIColor purpleColor]]; 129 [btn addTarget:self action:@selector(gotoNextView) forControlEvents:UIControlEventTouchUpInside]; 130 [self.view addSubview:btn]; 131 132 //想通知中心注册观察者 133 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSecret:) name:@"passValue" object:nil]; 134 135 } 136 // 通知回调 137 - (void)getSecret:(NSNotification *)notification 138 { 139 if ([notification.object isKindOfClass:[NSString class]]) { 140 self.lblNotification.text = notification.object; 141 } 142 } 143 //通知传值按钮关联方法 144 - (void)gotoNextView 145 { 146 NotificationViewController *notificationVC = [[NotificationViewController alloc] init]; 147 [self presentViewController:notificationVC animated:YES completion:nil]; 148 } 149 #pragma mark - gif动态图 150 - (void)createGifImageView 151 { 152 self.gifImgView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 100, 100)]; 153 //创建图片数组 154 NSMutableArray *imgArr = [NSMutableArray array]; 155 for (int i = 0; i < 80; i++) { 156 //获取图片名称 157 NSString *imgName = [NSString stringWithFormat:@"0%dda.png",i]; 158 //根据图片名称获取图片 159 UIImage *image = [UIImage imageNamed:imgName]; 160 //把图片加载到图片数组中 161 [imgArr addObject:image]; 162 } 163 //设置动画图片 164 [self.gifImgView setAnimationImages:imgArr]; 165 //播放时间 166 [self.gifImgView setAnimationDuration:8]; 167 //重复次数 168 [self.gifImgView setAnimationRepeatCount:0]; 169 //开启动画 170 [self.gifImgView startAnimating]; 171 [self.view addSubview:self.gifImgView]; 172 } 173 174 #pragma mark - 拾取器 175 - (void)createPickerView 176 { 177 self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 150, 355, 300)]; 178 self.pickerView.dataSource = self; 179 self.pickerView.delegate = self; 180 [self.view addSubview:self.pickerView]; 181 182 NSArray *arr1 = @[@"苹果",@"香蕉",@"橘子",@"葡萄",@"菩提"]; 183 NSArray *arr2 = @[@"汽车",@"飞机",@"轮船",@"自行车",@"呵呵"]; 184 NSArray *arr3 = @[@"11",@"22",@"33",@"44",@"55"]; 185 186 self.pickerDataSource = [[NSMutableArray alloc] initWithObjects:arr1,arr2,arr3, nil]; 187 //拾取器显示标签 188 self.lblShowPickerView = [[UILabel alloc] initWithFrame:CGRectMake(10, 500, 355, 80)]; 189 [self.lblShowPickerView setBackgroundColor:[UIColor orangeColor]]; 190 self.lblShowPickerView.textAlignment =NSTextAlignmentCenter; 191 [self.view addSubview:self.lblShowPickerView]; 192 //拾取器显示标签arr1 193 self.lblShowPickerView1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 115, 80)]; 194 [self.lblShowPickerView1 setBackgroundColor:[UIColor orangeColor]]; 195 self.lblShowPickerView1.textAlignment =NSTextAlignmentCenter; 196 [self.lblShowPickerView addSubview:self.lblShowPickerView1]; 197 198 //拾取器显示标签arr2 199 self.lblShowPickerView2 = [[UILabel alloc] initWithFrame:CGRectMake(115, 0, 115, 80)]; 200 [self.lblShowPickerView2 setBackgroundColor:[UIColor orangeColor]]; 201 self.lblShowPickerView2.textAlignment =NSTextAlignmentCenter; 202 [self.lblShowPickerView addSubview:self.lblShowPickerView2]; 203 204 //拾取器显示标签arr3 205 self.lblShowPickerView3 = [[UILabel alloc] initWithFrame:CGRectMake(230, 0, 115, 80)]; 206 [self.lblShowPickerView3 setBackgroundColor:[UIColor orangeColor]]; 207 self.lblShowPickerView3.textAlignment =NSTextAlignmentCenter; 208 [self.lblShowPickerView addSubview:self.lblShowPickerView3]; 209 210 211 } 212 #pragma mark - 拾取器的数据源方法 213 // 创建分组 214 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 215 { 216 return self.pickerDataSource.count; 217 } 218 // 创建分行 219 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 220 { 221 return [[self.pickerDataSource objectAtIndex:component] count]; 222 223 } 224 #pragma mark - 拾取器的代理方法 225 // 设置组宽 226 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 227 { 228 return 80; 229 } 230 // 设置行高 231 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 232 { 233 return 50; 234 } 235 // 设置行内容 236 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 237 { 238 return [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; 239 } 240 // 选中内容后调用 241 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 242 { 243 switch (component) { 244 case 0: 245 self.lblShowPickerView1.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; 246 break; 247 case 1: 248 self.lblShowPickerView2.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; 249 break; 250 case 2: 251 self.lblShowPickerView3.text = [[self.pickerDataSource objectAtIndex:component] objectAtIndex:row]; 252 break; 253 254 default: 255 break; 256 } 257 } 258 259 #pragma mark - 日期拾取器 260 - (void)createDatePicker 261 { 262 //日期拾取器(ps:拾取器的高度锁定不变) 263 self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 200, 355, 300)]; 264 //不管设置日期拾取器的日期格式如何,拾取器实际确定的时间都是完整的时刻 265 self.datePicker.datePickerMode = UIDatePickerModeDate; 266 [self.datePicker setDate:[NSDate date]]; 267 [self.datePicker addTarget:self action:@selector(dateChange) forControlEvents:UIControlEventValueChanged]; 268 [self.view addSubview:self.datePicker]; 269 //日期显示标签 270 self.lblShowDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 500, 355, 80)]; 271 [self.lblShowDate setBackgroundColor:[UIColor orangeColor]]; 272 self.lblShowDate.textAlignment = NSTextAlignmentCenter; 273 [self.view addSubview:self.lblShowDate]; 274 } 275 #pragma mark - 日期拾取器关联方法 276 - (void)dateChange 277 { 278 NSLog(@"%@",self.datePicker.date); 279 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 280 [formatter setDateFormat:@"YYYY-MM-dd HH:MM:ss"]; 281 NSString *dateStr = [formatter stringFromDate:self.datePicker.date]; 282 self.lblShowDate.text = dateStr; 283 284 } 285 286 #pragma mark - 状态栏 287 288 - (BOOL)prefersStatusBarHidden 289 { 290 return YES; 291 } 292 293 294 @end
1 #import <UIKit/UIKit.h> 2 3 @interface NotificationViewController : UIViewController 4 5 @end 6 7 8 #import "NotificationViewController.h" 9 10 @interface NotificationViewController () 11 12 @end 13 14 @implementation NotificationViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 [self.view setBackgroundColor:[UIColor cyanColor]]; 19 20 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 21 [btn setFrame:CGRectMake(30, 240, 315, 50)]; 22 [btn setTitle:@"passValue" forState:UIControlStateNormal]; 23 [btn setBackgroundColor:[UIColor purpleColor]]; 24 [btn addTarget:self action:@selector(passValue) forControlEvents:UIControlEventTouchUpInside]; 25 [self.view addSubview:btn]; 26 27 28 } 29 - (void)passValue 30 { 31 //返回上一次模态视图 32 [self dismissViewControllerAnimated:YES completion:nil]; 33 34 NSString *str = @"here is a secret"; 35 36 //发送带消息的通知 37 [[NSNotificationCenter defaultCenter] postNotificationName:@"passValue" object:str]; 38 } 39 40 41 @end
1 #import <UIKit/UIKit.h> 2 3 @protocol PassValueDelegate <NSObject> 4 5 - (void)my_passValue:(NSString *)str; 6 7 @end 8 9 @interface DelegatePassValueViewController : UIViewController 10 11 //实现协议的代理 12 @property(strong, nonatomic) id<PassValueDelegate> delegate; 13 14 @end 15 16 #import "DelegatePassValueViewController.h" 17 18 @interface DelegatePassValueViewController () 19 20 @end 21 22 @implementation DelegatePassValueViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 [self.view setBackgroundColor:[UIColor cyanColor]]; 27 28 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 29 [btn setFrame:CGRectMake(30, 240, 315, 50)]; 30 [btn setTitle:@"passValueBack" forState:UIControlStateNormal]; 31 [btn setBackgroundColor:[UIColor purpleColor]]; 32 [btn addTarget:self action:@selector(passValueBack) forControlEvents:UIControlEventTouchUpInside]; 33 [self.view addSubview:btn]; 34 } 35 36 - (void)passValueBack 37 { 38 [self dismissViewControllerAnimated:YES completion:nil]; 39 40 NSString *str = @"another secret"; 41 //让代理执行协议方法 42 if ([self.delegate respondsToSelector:@selector(my_passValue:)]) { 43 [self.delegate performSelector:@selector(my_passValue:) withObject:str]; 44 } 45 } 46 47 48 @end
笔记
1 一.UIPickerView 2 1.UIPickerView的常见属性 3 // 数据源(用来告诉UIPickerView有多少列多少行) 4 @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; 5 // 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择) 6 @property(nonatomic,assign) id<UIPickerViewDelegate> delegate; 7 // 是否要显示选中的指示器 8 @property(nonatomic) BOOL showsSelectionIndicator; 9 // 一共有多少列 10 @property(nonatomic,readonly) NSInteger numberOfComponents; 11 12 2.UIPickerView的常见方法 13 // 重新刷新所有列 14 - (void)reloadAllComponents; 15 // 重新刷新第component列 16 - (void)reloadComponent:(NSInteger)component; 17 18 // 主动选中第component列的第row行 19 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; 20 21 // 获得第component列的当前选中的行号 22 - (NSInteger)selectedRowInComponent:(NSInteger)component; 23 24 3.数据源方法(UIPickerViewDataSource) 25 // 一共有多少列 26 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 27 // 第component列一共有多少行 28 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 29 30 4.代理方法(UIPickerViewDelegate) 31 // 第component列的宽度是多少 32 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; 33 // 第component列的行高是多少 34 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; 35 36 // 第component列第row行显示什么文字 37 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 38 39 // 第component列第row行显示怎样的view(内容) 40 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 41 42 // 选中了pickerView的第component列第row行 43 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 44 45 二.UIDatePicker 46 1.常见属性 47 // datePicker的显示模式 48 @property (nonatomic) UIDatePickerMode datePickerMode; 49 // 显示的区域语言 50 @property (nonatomic, retain) NSLocale *locale; 51 52 2.监听UIDatePicker的选择 53 * 因为UIDatePicker继承自UIControl,所以通过addTarget:...监听 54 55 三.程序启动的完整过程 56 1.main函数 57 58 2.UIApplicationMain 59 * 创建UIApplication对象 60 * 创建UIApplication的delegate对象 61 62 3.delegate对象开始处理(监听)系统事件(没有storyboard) 63 * 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法 64 * 在application:didFinishLaunchingWithOptions:中创建UIWindow 65 * 创建和设置UIWindow的rootViewController 66 * 显示窗口 67 68 3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard) 69 * 创建UIWindow 70 * 创建和设置UIWindow的rootViewController 71 * 显示窗口