[TOC]
UIView与UIWindow之间的关系
在IOS中使用window和view来展现应用程序的内容。windows是不可见的部分但是它提供了一个基本的容器用来放置程序的视图(view)。每一个app都至少包含一个window以及一个view来展现该app的内容
新建UIWindow
//在AppDelegate.m中
#import "ViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *root = [[ViewController alloc] init];
self.window.rootViewController = root;
return YES;
}
创建UIView
//新建UIView
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 120, 40)];
//设置背景色
view1.backgroundColor = [UIColor redColor];
//添加子视图
[self.view addSubview:view1];
UIView常见属性和方法
//1透明度(0-1)
view1.alpha = 0.5;
//2是否隐藏
view1.hidden = NO;
//3中心点
view1.center = self.view.center;
//4tag
view1.tag = 99;
//根据编号找view1
UIView *resultView = [self.view viewWithTag:99];
resultView.backgroundColor = [UIColor blackColor];
//将一个字视图移动到前面
[self.view bringSubviewToFront:resultView];
//将一个字视图移动到后面
[self.view sendSubviewToBack:resultView];
//子视图自杀
[view2 removeFromSuperview];
UIButton
- (void)viewDidLoad {
[super viewDidLoad];
/*typedef enum {
UIButtonTypeCustom = 0, 自定义风格
UIButtonTypeRoundedRect, 圆角矩形
UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight, 亮色感叹号
UIButtonTypeInfoDark, 暗色感叹号
UIButtonTypeContactAdd, 十字加号按钮
} UIButtonType;*/
/*
enum {
UIControlStateNormal = 0, 常规状态显现
UIControlStateHighlighted = 1 << 0, 高亮状态显现
UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
UIControlStateSelected = 1 << 2, 选中状态
UIControlStateApplication = 0x00FF0000, 当应用程序标志时
UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管
};*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor greenColor];
[self.view addSubview:button];
//位置大小
button.frame = CGRectMake(10 , 100, 100, 30);
//内容
[button setTitle:@"按钮" forState:UIControlStateNormal];
//字体颜色
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize: 14.0];
//文字位置
button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
//文字自动适应button大小
button.titleLabel.adjustsFontSizeToFitWidth = YES;
//设置按钮按下会发光
button.showsTouchWhenHighlighted=NO;
//背景图片
//创建一张图片
UIImage *image = [UIImage imageNamed:@"haha"];
[button setBackgroundImage:image forState:UIControlStateNormal];
//前景图片
[button setImage:[UIImage imageNamed:@"hh"] forState:UIControlStateNormal];
//调整文字和图片位置
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
//添加点击事件
[button addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];
}
//实现按钮到点击方法
- (void)youTouchMe
{
NSLog(@"你摸了我");
}
UILabel
- (void)viewDidLoad {
[super viewDidLoad];
//创建UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 260, 260)];
//设置背景色
label.backgroundColor = [UIColor yellowColor];
//把标签添加到页面上
[self.view addSubview:label];
//内容
label.text = @"I'm LabelI'm LabelI'm LabelI'm LabelI'm LabelI'm LabelI'm LabelI'm Label";
//文字居中
label.textAlignment = NSTextAlignmentCenter;
//文字颜色
label.textColor = [UIColor redColor];
//字体大小
label.font = [UIFont systemFontOfSize:30];
//粗体字体
label.font = [UIFont boldSystemFontOfSize:30];
//字体格式
// label.font = [UIFont fontWithName:@"隶书" size:30];
//文字阴影和偏移量
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(2, 2);
//显示行数
label.numberOfLines = 4;
//设置圆角
label.layer.cornerRadius = 20;
label.layer.masksToBounds = YES;
}
UITextField
//条件1 签订协议
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//输入框
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 260, 50)];
field.backgroundColor = [UIColor yellowColor];
[self.view addSubview:field];
//提示
field.placeholder = @"请输入密码";
field.textColor = [UIColor redColor];
field.font = [UIFont systemFontOfSize:25];
//暗文输入
field.secureTextEntry = YES;
//右侧清空按钮
field.clearButtonMode = UITextFieldViewModeAlways;
//是否允许输入
field.enabled = YES;
//键盘类型
// field.keyboardType = UIKeyboardTypeNumberPad;
//边框
field.borderStyle = UITextBorderStyleRoundedRect;
//自定义边框
// field.layer.borderColor = [UIColor redColor].CGColor;
// field.layer.borderWidth = 2;
// field.layer.cornerRadius = 5;
// field.layer.masksToBounds = YES;
//return键的样式
field.returnKeyType = UIReturnKeyGo;
//代理方法
//条件2 指定代理人
field.delegate = self;
}
//UITextField的一个代理方法
//条件3 使用协议的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"你点了return键");
//回收键盘,取消第一响应者
// [textField resignFirstResponder];
[textField endEditing:YES];
NSString *str = textField.text;
if([str isEqualToString:@"123"]){
NSLog(@"ok");
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"开始编辑");
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"结束编辑");
}
ViewController
要创建一个viewcontroller类
- (void)viewDidLoad {
[super viewDidLoad];
//第一个红色页面
NSLog(@"视图DidLoad");
self.view.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 100, 100, 50);
[self.view addSubview:button];
button.backgroundColor = [UIColor yellowColor];
[button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
}
- (void) jump
{
//跳转第二个页面
SecondViewController *second = [[SecondViewController alloc] init];
//跳转方式
second.modalTransitionStyle =
UIModalTransitionStyleFlipHorizontal;
//跳!!!
[self presentViewController:second animated:YES completion:^{
NSLog(@"跳转完成");
}];
}
- (void)loadView
{
[super loadView];
NSLog(@"视图loadView");
}
- (void) viewWillAppear:(BOOL)animated
{
NSLog(@"视图viewWillAppear");
}
- (void) viewDidAppear:(BOOL)animated
{
NSLog(@"视图DidAppear");
}
- (void) viewDidDisappear:(BOOL)animated
{
NSLog(@"视图DidDisappear");
}
![image](http://note.youdao.com/favicon.ico)
UIImageView
- (void)viewDidLoad {
[super viewDidLoad];
//图片类
UIImageView *imagev = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
imagev.backgroundColor = [UIColor yellowColor];
[self.view addSubview:imagev];
//创建一个图片
UIImage *ima = [UIImage imageNamed:@"1.jpg"];
//显示
imagev.image = ima;
//逐帧动画
//设置播放资源(图片放到数组中)
NSMutableArray *array = [NSMutableArray array];
for (int i=0; i<=80; i++) {
NSString *name = [NSString stringWithFormat:@"drink_%d.jpg", i];
UIImage *image = [UIImage imageNamed:name];
[array addObject:image];
}
imagev.animationImages = array;
//设置时间
imagev.animationDuration = 3;
//设置重复次数
imagev.animationRepeatCount = 2;
//启动
[imagev startAnimating];
// [imagev stopAnimating];
}
自定义视图
1 首先新建一个UIView
的子类命名为MyView
2 然后在
MyView.m
中重写initWithFrame:
方法 并延展私有属性 完成MyView
的封装
#import <UIKit/UIKit.h>
@interface MyView : UIView
//声明set方法
- (void)setLabelText:(NSString *) NSString;
- (void)setTextFieldPlaceholder:(NSString *)NSString;
@end
#import "MyView.h"
//延展私有属性
@interface MyView()
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UITextField *tf;
@end
@implementation MyView
//实现set方法
- (void)setLabelText:(NSString *)NSString
{
_label.text = NSString;
}
- (void)setTextFieldPlaceholder:(NSString *)NSString
{
_tf.placeholder = NSString;
}
//重写初始化方法 完成布局
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 80, 40)];
_tf = [[UITextField alloc] initWithFrame:CGRectMake(90, 5, 240, 40)];
_label.backgroundColor = [UIColor yellowColor];
_tf.backgroundColor = [UIColor redColor];
_label.layer.cornerRadius = 10;
_label.layer.masksToBounds = YES;
_tf.layer.cornerRadius = 10;
_tf.layer.masksToBounds = YES;
self.backgroundColor = [UIColor blueColor];
[self addSubview:_label];
[self addSubview:_tf];
}
return self;
}
3 在View中使用自定义视图
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一步新建一个类继承自UIView
//第二步分析新视图由那一些旧识图构成 并且声明为属性
//第三步重写初始化方法 在方法里完成它的布局
//使用自定义的视图
MyView *view = [[MyView alloc] initWithFrame:CGRectMake(10, 100, 360, 50)];
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
[self.view addSubview:view];
//延展里写的私有属性 不允许在外界直接修改
// view.label.text = @"用户名";
// view.tf.placeholder = @"请输入用户名...";
[view setLabelText:@"用户名"];
[view setTextFieldPlaceholder:@"请输入用户名..."];
}
-
效果图如下
UIAlertController
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate,UIActionSheetDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"是否18" delegate:self cancelButtonTitle:@"不满足" otherButtonTitles:@"满足", nil];
// //展示
// [alt show];
//
// UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择城市" delegate:self cancelButtonTitle:@"青海"destructiveButtonTitle:@"西安" otherButtonTitles:@"上海", @"深圳"
// , nil];
// [sheet showInView:self.view];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 100, 120, 60);
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(touchMe) forControlEvents:UIControlEventTouchUpInside];
}
//UIAlertViewDelegate代理方法区分选项
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
// NSLog(@"%ld", buttonIndex);
// if (buttonIndex == 1) {
// NSLog(@"ok");
// }
//}
////UIActionSheetDelegate代理方法区分选项
//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
// NSLog(@"%ld", buttonIndex);
// if (buttonIndex == 1) {
// NSLog(@"打印鸡排");
// }
//}
- (void)touchMe
{
//new提示框
UIAlertController *control = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"是否购买wawa" preferredStyle:UIAlertControllerStyleAlert];
//添加选项
UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"是否购买" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"买了");
}];
UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"不买" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"不买");
}];
//红色
UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"重点" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"it`s me");
}];
[control addAction:act1];
[control addAction:act2];
[control addAction:act3];
[self presentViewController:control animated:YES completion:^{
NSLog(@"ok");
}];
//定时器
// [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeOut) userInfo:nil repeats:NO];
}
- (void)timeOut
{
NSLog(@"时间到");
[self dismissViewControllerAnimated:YES completion:^{
}];
}
事件
在ViewController.h中添加UITextFiled并添加子视图MyView完成点击空白区域收起键盘功能
#import "ViewController.h"
#import "MyView.h"
//将tf设置为全局变量
@interface ViewController ()
{
UITextField *tf;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tf= [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 60)];
tf.backgroundColor = [UIColor redColor];
[self.view addSubview:tf];
MyView *view = [[MyView alloc] initWithFrame:CGRectMake(20, 300, 270, 200)];
view.backgroundColor = [UIColor grayColor];
//关闭用户交互
// view.userInteractionEnabled = NO;
[self.view addSubview:view];
}
//触摸屏幕的 触摸方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸开始");
//关闭第一响应者
[tf resignFirstResponder];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸结束");
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"移动");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"取消触摸");
}
//摇晃手机触发方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇晃开始");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇晃结束");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇晃取消");
}
在MyView中实现触摸方法
//触摸屏幕的 触摸方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸view开始");
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸view结束");
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"view移动");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"view取消触摸");
}
效果图如下
当点击空白区域键盘收起
点击灰色区域输出
点击白色区域
UISegmentedControl
- (void)viewDidLoad {
[super viewDidLoad];
//创建分段控件
NSArray *array = [NSArray arrayWithObjects:@"亚洲",@"欧美",@"日韩",nil];
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:array];
seg.frame = CGRectMake(30, 100, 300, 50);
[self.view addSubview:seg];
//修改颜色
seg.tintColor = [UIColor redColor];
//默认选中某个
seg.selectedSegmentIndex = 1;
//修改标题
[seg setTitle:@"动漫" forSegmentAtIndex:2];
// //设置选项图片
// UIImage *image = [UIImage imageNamed:@"111"];
// //防止图片被控件渲染
// [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// [seg setImage:image forSegmentAtIndex:0];
//修改字体颜色
//添加事件
[seg addTarget:self action:@selector(selectWhich:) forControlEvents:UIControlEventValueChanged];
}
- (void)selectWhich:(UISegmentedControl *)se
{
NSLog(@"%ld", (long)se.selectedSegmentIndex);
}
UISwith,UISlider
#import "ViewController.h"
@interface ViewController ()
{
UITextField *tf;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//开关UISwitch
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
[self.view addSubview:sw];
//设置开关颜色
// sw.onTintColor = [UIColor yellowColor];
//设置小球颜色
// sw.thumbTintColor = [UIColor orangeColor];
//默认开关视打开的
sw.on = YES;
//给开关添加事件
[sw addTarget:self action:@selector(touchSwitch:) forControlEvents:UIControlEventValueChanged];
tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 260, 40)];
tf.backgroundColor = [UIColor blueColor];
[self.view addSubview:tf];
//滑动条UISlider
UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(100, 400, 260, 20)];
// sl.backgroundColor = [UIColor purpleColor];
//小球的颜色
sl.thumbTintColor = [UIColor grayColor];
//未滑过区域颜色
sl.maximumTrackTintColor = [UIColor redColor];
//滑过区域颜色
sl.minimumTrackTintColor = [UIColor greenColor];
//设置最大最小值
sl.maximumValue = 100.0;
sl.minimumValue = 0.0;
//添加事件
[sl addTarget:self action:@selector(sliderMe:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sl];
}
- (void)sliderMe:(UISlider *)s
{
NSLog(@"%f", s.value);
}
- (void)touchSwitch:(UISwitch *)s
{
tf.enabled = s.on;
}
UIScrollView,UIPageControl
@interface ViewController ()<UIScrollViewDelegate>
{
UIPageControl *PControl;
UIScrollView *sv;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 414, 736)];
sv.backgroundColor = [UIColor yellowColor];
[self.view addSubview:sv];
//设置大小
// NSLog(@"%@",self.view);
sv.contentSize = CGSizeMake(414*4, 736);
//把图片放进去
for (int i = 1; i<=4; i++) {
NSString *name = [NSString stringWithFormat:@"%d.jpg",i];
UIImage *image = [UIImage imageNamed:name];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(414*(i-1), 0, 414, 736)];
imageV.image = image;
[sv addSubview:imageV];
}
//分页显示
sv.pagingEnabled = YES;
//是否允许反弹
// sv.bounces = NO;
//修改滚动条
sv.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//隐藏滚动条
// sv.showsHorizontalScrollIndicator = YES;
// sv.showsVerticalScrollIndicator = NO;
//滑动到指定位置(偏移量)
sv.contentOffset = CGPointMake(414*3, 0);
sv.delegate = self;
//创建下面点四个小点
PControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 700, 414, 50)];
// PControl.backgroundColor = [UIColor redColor];
[self.view addSubview:PControl];
//几个点
PControl.numberOfPages = 4;
//未选中点的颜色
// PControl.pageIndicatorTintColor = [UIColor yellowColor];
//选中点的颜色
// PControl.currentPageIndicatorTintColor = [UIColor blueColor];
//添加事件
[PControl addTarget:self action:@selector(pageCon:) forControlEvents:UIControlEventValueChanged];
}
- (void)pageCon:(UIPageControl *)p
{
NSLog(@"--->>%ld",p.currentPage);
// sv.contentOffset = CGPointMake(414*p.currentPage, 0);
[sv setContentOffset:CGPointMake(414*p.currentPage, 0) animated:YES];
}
//UIScrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// NSLog(@"动了");
// NSLog(@"%d", number + 1);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"开始拖拽");
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
NSLog(@"结束拖拽");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"开始减速");
NSInteger number = (NSInteger)scrollView.contentOffset.x/414;
//设置底部的小点到相应的位置
PControl.currentPage = number;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"结束减速");
}
UINavigationController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController *root = [[ViewController alloc] init];
//创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
//把导航控制器作为window的根视图
self.window.rootViewController = nav;
//导航栏的颜色
nav.navigationBar.barTintColor = [UIColor blueColor];
//半透明
nav.navigationBar.translucent = NO;
return YES;
}
#import "ViewController.h"
#import "SeViewController.h"
@interface ViewController ()<chuanZhiDelegate>
{
UITextField *tf;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//把控制导航栏内容的代码写到对应的页面来
self.navigationItem.title = @"首页";
// self.navigationItem.titleView =
//左右侧的按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧" style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more"] style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];
//第二个页面
tf = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 280, 40)];
tf.backgroundColor = [UIColor grayColor];
[self.view addSubview:tf];
}
- (void)touchRight
{
NSLog(@"点了右面");
SeViewController *se = [[SeViewController alloc] init];
//传值
se.inputStr = tf.text;
//指定代理人
se.delegate = self;
//跳转
[self.navigationController pushViewController:se animated:YES];
}
- (void)chuanZhi:(NSString *)str
{
NSLog(@"验证有没有传递成功%@",str);
}
#import <UIKit/UIKit.h>
//第一步 协议的名称 声明协议
@protocol chuanZhiDelegate <NSObject>
//第三步 写方法
- (void)chuanZhi:(NSString *)str;
@end
@interface SeViewController : UIViewController
//声明一个属性来接收传来的值
@property(nonatomic,strong)NSString *inputStr;
//第二步 声明属性(代理人)
@property(nonatomic,assign)id<chuanZhiDelegate>delegate;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
//第二个页面左上角的返回按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
//
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 280, 40)];
label.backgroundColor = [UIColor grayColor];
label.text = self.inputStr;
[self.view addSubview:label];
}
- (void)goBack
{
//返回时触发代理方法 将值传递回去
[self.delegate chuanZhi:@"这是个返回的值"];
[self.navigationController popViewControllerAnimated:YES];
}
Block和单例传值
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
nav.navigationBar.barTintColor = [UIColor greenColor];
nav.navigationBar.translucent = NO;
return YES;
}
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "MyData.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToNext)];
//创建一个单例类对象并赋值
MyData *data = [MyData instance];
data.name = @"赵日天";
//通知
//注册通知 (打开收音机 并调到对应的频率)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMessage:) name:@"99.7" object:nil];
}
- (void)receiveMessage:(NSNotification *)not
{
NSLog(@"%@", not.object);
}
- (void)viewDidDisappear:(BOOL)animated
{
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"99.7" object:nil];
}
- (void)jumpToNext
{
SecondViewController *second = [[SecondViewController alloc] init];
__block ViewController *tempSelf = self;
second.chuanzhiBlock = ^(NSString *str){
NSLog(@"%@",str);
tempSelf.navigationItem.title = @"111";
};
[self.navigationController pushViewController:second animated:YES];
}
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(nonatomic,copy)void(^chuanzhiBlock)(NSString *str);
@end
SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(jumpBack)];
MyData *data = [MyData instance];
NSLog(@"%@",data.name);
}
- (void)jumpBack
{
self.chuanzhiBlock(@"block将值传回");
//发送广播(通知)
[[NSNotificationCenter defaultCenter] postNotificationName:@"99.7" object:@"通知传值" userInfo:nil];
[self.navigationController popViewControllerAnimated:YES];
}
MyData.h
#import <Foundation/Foundation.h>
@interface MyData : NSObject
@property(nonatomic,strong)NSString *name;
+ (MyData *)instance;
@end
MyData.m
#import "MyData.h"
@implementation MyData
static MyData *mydata = nil;
+ (MyData *)instance
{
if (mydata == nil) {
mydata = [[MyData alloc] init];
}
return mydata;
}
@end
UITaBarController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//淘宝页面
TaoBaoViewController *taobao = [[TaoBaoViewController alloc] init];
UINavigationController *taoNav = [[UINavigationController alloc] initWithRootViewController:taobao];
// UITabBarController *tabbar = [[UITabBarController alloc] init];
//设置标题
taoNav.tabBarItem.title = @"淘宝";
//设置未选中图片
taoNav.tabBarItem.image = [UIImage imageNamed:@"淘宝"];
//设置选中图片
// taoNav.tabBarItem.selectedImage = [UIImage imageNamed:@""];
//天猫页面
TmallViewController *tmall = [[TmallViewController alloc] init];
UINavigationController *tmallNav = [[UINavigationController alloc] initWithRootViewController:tmall];
UITabBarController *tabbarC = [[UITabBarController alloc] init];
tmallNav.tabBarItem.title = @"天猫";
tmallNav.tabBarItem.image = [UIImage imageNamed:@"天猫"];
//把页面放到tabbarcontroller中
tabbarC.viewControllers = [NSArray arrayWithObjects:taoNav,tmallNav, nil];
//把tabbarcontroller作为window的根视图
self.window.rootViewController = tabbarC;
//统一设置导航栏样式
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
[[UINavigationBar appearance] setTranslucent:NO];
return YES;
}
UITableView
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
nav.navigationBar.barTintColor = [UIColor greenColor];
nav.navigationBar.translucent = NO;
self.window.rootViewController = nav;
return YES;
}
Movie.h
#import <Foundation/Foundation.h>
@interface Movie : NSObject
@property(nonatomic,strong)NSString *movieName;
@property(nonatomic,strong)NSString *movieActor;
@end
ViewController.m
#import "ViewController.h"
#import "Movie.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 414, 736) style:UITableViewStylePlain];
[self.view addSubview:tab];
tab.delegate = self;
tab.dataSource = self;
//添加表头
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 80)];
imageV.image = [UIImage imageNamed:@"4.jpg"];
tab.tableHeaderView = imageV;
//数组源数据
// array = [NSArray arrayWithObjects:@"亚瑟",@"剑神",@"盲僧",@"老牛",@"小灰", nil];
array = [NSMutableArray array];//初始化
Movie *m1 = [[Movie alloc] init];
m1.movieName = @"金瓶梅";
m1.movieActor = @"张大大";
Movie *m2 = [[Movie alloc] init];
m2.movieName = @"终结者";
m2.movieActor = @"李大大";
//将电影添加到数组中
[array addObject:m1];
[array addObject:m2];
}
//代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1 创建标识符
static NSString *identifier = @"cell";
//2 根据标识符从重用池取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//3 如果没有就创建一个新的
if (cell == nil) {
NSLog(@"经来了");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
//对cell进行赋值
//这么写娄
// cell.textLabel.text = [array objectAtIndex:indexPath.row];
// cell.detailTextLabel.text = @"666666";
//这样写好
Movie *tempMovie = [array objectAtIndex:indexPath.row];
cell.textLabel.text = tempMovie.movieName;
cell.detailTextLabel.text = tempMovie.movieActor;
cell.imageView.image = [UIImage imageNamed:@"淘宝"];
cell.accessoryType = UITableViewCellAccessoryDetailButton;
// cell.accessoryView =
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count ;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//控制分区代理方法(亚洲,欧美,日韩)
//分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
//分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
//标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"分区";
}
//自定义分区
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 414, 40)];
view.backgroundColor = [UIColor redColor];
return view;
}
UITableView的编辑
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *root = [[ViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
nav.navigationBar.barTintColor = [UIColor grayColor];
nav.navigationBar.translucent = NO;
return YES;
}
ViewController.m
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *dataArray;
UITableView *tab;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 414, 736-64) style:UITableViewStylePlain];
[self.view addSubview:tab];
tab.delegate = self;
tab.dataSource = self;
dataArray = [NSMutableArray array];
[dataArray addObject:@"宋江"];
[dataArray addObject:@"武松"];
[dataArray addObject:@"王英"];
[dataArray addObject:@"老虎"];
//系统编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//点击编辑按钮执行的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//开启tab编辑
[tab setEditing:editing animated:animated];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
//控制编辑的代理方法
//是否允许编辑
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.row == 3) {
// return NO;
// }
// return YES;
//}
//允许移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//插入还是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
//控制删除和插入操作的
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判断你要干嘛
if (editingStyle == UITableViewCellEditingStyleDelete) {
//这里写删除的代码
//1 先删除数组中的数据
[dataArray removeObjectAtIndex:indexPath.row];
//2 删除cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
//控制移动的代码
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//移动的代码写在这
//1 获取要移动的数据
NSString *str = [dataArray objectAtIndex:sourceIndexPath.row];
//2 删除这一行
[dataArray removeObjectAtIndex:sourceIndexPath.row];
//3 插入到新的位置
[dataArray insertObject:str atIndex:destinationIndexPath.row];
}
//点击cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"你点了第%ld行",indexPath.row+1);
//刷新列表
[tab reloadData];
}
自定义cell
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *root = [[ViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
nav.navigationBar.barTintColor = [UIColor grayColor];
nav.navigationBar.translucent = NO;
return YES;
}
ViewController.m
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *dataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 414, 736-64) style:UITableViewStylePlain];
[self.view addSubview:tab];
tab.delegate = self;
tab.dataSource = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
//调用自己写的控件
cell.movieIcon.image = [UIImage imageNamed:@""];
cell.movieName.text = @"湄公河";
cell.movieActor.text = @"网民";
cell.movieTime.text = @"60";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
MyTableViewCell.h
#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
//1 定义需要的属性 (空件)
@property(nonatomic,strong)UIImageView *movieIcon;
@property(nonatomic,strong)UILabel *movieName;
@property(nonatomic,strong)UILabel *movieActor;
@property(nonatomic,strong)UILabel *movieTime;
@end
MyTableViewCell.m
#import "MyTableViewCell.h"
@implementation MyTableViewCell
//2 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//在初始化方法中完成控件的初始化 并添加到cell上
self.movieIcon = [[UIImageView alloc] init];
[self.contentView addSubview:self.movieIcon];
self.movieName = [[UILabel alloc] init];
[self.contentView addSubview:self.movieName];
self.movieActor = [[UILabel alloc] init];
[self.contentView addSubview:self.movieActor];
self.movieTime = [[UILabel alloc] init];
[self.contentView addSubview:self.movieTime];
}
return self;
}
//3 设置控件的样式
- (void)layoutSubviews
{
[super layoutSubviews];
self.movieIcon.frame = CGRectMake(10, 10, 50, 50);
self.movieName.frame = CGRectMake(70, 10, 260, 20);
self.movieActor.frame = CGRectMake(70, 40, 260, 20);
self.movieTime.frame = CGRectMake(70, 70, 260, 20);
self.movieIcon.backgroundColor = [UIColor redColor];
self.movieName.backgroundColor = [UIColor grayColor];
self.movieActor.backgroundColor = [UIColor grayColor];
self.movieTime.backgroundColor = [UIColor grayColor];
}