UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
来源:互联网 发布:android 相机源码 编辑:程序博客网 时间:2024/06/05 07:36
一.UINavigationController
// 创建一个导航控制器
// 创建一个控制器作为根控制器 去管理
RootViewController *rootVC = [[RootViewControlleralloc]init];
UINavigationController *navC = [[UINavigationControlleralloc]initWithRootViewController:rootVC];
[rootVC release];
// 把导航控制器作为window的根控制器
self.window.rootViewController= navC;
[navCrelease];
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColorgreenColor];
// 创建一个按钮 进入下一个页面
UIButton *button = [UIButtonbuttonWithType:(UIButtonTypeCustom)];
button.frame= CGRectMake(100,100,100,100);
[self.viewaddSubview:button];
button.backgroundColor= [UIColorredColor];
[button setTitle:@"2"forState:(UIControlStateNormal)];
[button release];
// 添加方法 进入下一个界面
[button addTarget:selfaction:@selector(actionButton:)forControlEvents:(UIControlEventTouchDragInside)];
[superviewDidLoad];
self.view.backgroundColor= [UIColorgreenColor];
// 创建一个按钮 进入下一个页面
UIButton *button = [UIButtonbuttonWithType:(UIButtonTypeCustom)];
button.frame= CGRectMake(100,100,100,100);
[self.viewaddSubview:button];
button.backgroundColor= [UIColorredColor];
[button setTitle:@"2"forState:(UIControlStateNormal)];
[button release];
// 添加方法 进入下一个界面
[button addTarget:selfaction:@selector(actionButton:)forControlEvents:(UIControlEventTouchDragInside)];
//实现Button的方法 进入下一个界面
- (void)actionButton:(UIButton*)button
{
// 创建下一个界面的
SecondViewController *secondVC = [[SecondViewControlleralloc]init];
// ViewControllers 是导航控制器管理的那一组控制器 所有被管理的控制器们都存放在这个数组中
// NSLog(@"%@", self.navigationController.viewControllers);
// 跳转去下一个页面 push
[self.navigationControllerpushViewController:secondVCanimated:YES];
[secondVC release];
// NSLog(@"%@", self.navigationController.viewControllers);
// 栈顶的控制器
// 正显示的控制器
NSLog(@"栈顶%@",self.navigationController.topViewController);
- (void)actionButton:(UIButton*)button
{
// 创建下一个界面的
SecondViewController *secondVC = [[SecondViewControlleralloc]init];
// ViewControllers 是导航控制器管理的那一组控制器 所有被管理的控制器们都存放在这个数组中
// NSLog(@"%@", self.navigationController.viewControllers);
// 跳转去下一个页面 push
[self.navigationControllerpushViewController:secondVCanimated:YES];
[secondVC release];
// NSLog(@"%@", self.navigationController.viewControllers);
// 栈顶的控制器
// 正显示的控制器
NSLog(@"栈顶%@",self.navigationController.topViewController);
}
//返回上一个界面
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (void)actionSecondButton:(UIButton*)button
{
// 返回根界面
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
- (void)actionThirdButton:(UIButton*)button
{
// 返回到指定界面
// 需要从被管理的数组中 把要返回的取出来
NSArray *viewControllers = self.navigationController.viewControllers;
SecondViewController *sec = viewControllers[1];
[self.navigationControllerpopToViewController:secanimated:YES];
// 或者下面的
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (void)actionSecondButton:(UIButton*)button
{
// 返回根界面
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
- (void)actionThirdButton:(UIButton*)button
{
// 返回到指定界面
// 需要从被管理的数组中 把要返回的取出来
NSArray *viewControllers = self.navigationController.viewControllers;
SecondViewController *sec = viewControllers[1];
[self.navigationControllerpopToViewController:secanimated:YES];
// 或者下面的
//[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
状态栏高 20
bar 高 44
*/
self.view.backgroundColor= [UIColorredColor];
self.navigationItem.title= @"首页";
// 创建一个UIView出来给titleView 有了View想往上面加什么都随意
//UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 44)];
// view.backgroundColor = [UIColor whiteColor];
//self.navigationItem.titleView = view;
// 用系统样式初始化一个UIBarButtonItem
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCameratarget:selfaction:@selector(actionLeftBarButtonItem:)];
self.navigationItem.leftBarButtonItem= leftButton;
[leftButton release];
// 用标题的初始化方法 初始化右面的按钮
UIBarButtonItem *rightBarButton = [[UIBarButtonItemalloc]initWithTitle:@"确定"style:(UIBarButtonItemStylePlain)target:selfaction:@selector(actionRightBarButtonItem:)];
self.navigationItem.rightBarButtonItem= rightBarButton;
// 用图片初始化UIBarButtonItem
UIBarButtonItem *imageBarButton = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"left"]style:(UIBarButtonItemStylePlain)target:selfaction:@selector(actionImageBarButton:)];
self.navigationItem.rightBarButtonItem= imageBarButton;
[imageBarButton release];
// 设置一下Bar的颜色及图片
// [self.navigationController.navigationBar setBarTintColor:[UIColor grayColor]];
// 高44的把 导航条遮住了,漏出了状态栏
// 高64的把 导航栏和状态栏都充满
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"32064"] forBarMetrics:(UIBarMetricsDefault)];
// 重要的属性
// 从7.0以后 导航条默认是半透明的 如果半透明 从屏幕左上角开始计算frame
// 如果设置成不透明的 从导航条的底部开始计算frame
// 透明度的设置
self.navigationController.navigationBar.translucent= NO;
//
// Do any additional setup after loading the view.
}
- (void)actionLeftBarButtonItem:(UIBarButtonItem*)barButtonItem
{
NSLog(@"启动照相机");
}
- (void)actionRightBarButtonItem:(UIBarButtonItem*)barButtonItem
{
NSLog(@"我是右面的ooo");
}
- (void)actionImageBarButton:(UIBarButtonItem*)barButtonItem
{
NSLog(@"图片的按钮");
二.界面传值
属性传值
* 从前往后(根到 顶)是用属性传值
1. 点击跳转方法的时候传值
2. 要传递的是什么?传递的是数据不是控件
1. 点击跳转方法的时候传值
2. 要传递的是什么?传递的是数据不是控件
*/
}// 要拿到要传递的数据
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
NSString *string = textField.text;
// 要有一个属性来接收这个数据
SecondViewController *second = [[SecondViewControlleralloc]init];
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
NSString *string = textField.text;
// 要有一个属性来接收这个数据
SecondViewController *second = [[SecondViewControlleralloc]init];
second.string = string;
代理传值
* 从后往前(顶到 根)用代理传值
1.协议从哪儿写? 后面的界面写
@protocolSecondDelegate <NSObject>
- (void)changeTextFieldString:(NSString*)string;
@end
@interfaceSecondViewController : UIViewController
@property(nonatomic,retain)NSString*string;
- (void)changeTextFieldString:(NSString*)string;
@end
@interfaceSecondViewController : UIViewController
@property(nonatomic,retain)NSString*string;
@end
2.协议中要有的方法 传值用 方法中的参数就是咱们要传得值
3.顶搞一个代理的属性出来 设置代理 来使用
@property(nonatomic,retain)id<SecondDelegate> delegate;
4.根遵守协议 然后 根设置代理 根实现方法
@interfaceRootViewController : UIViewController<SecondDelegate>
- (void)changeTextFieldString:(NSString*)string
{
SecondViewController *second = [[SecondViewControlleralloc]init];
// 设置代理
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
textField.text= string;
{
SecondViewController *second = [[SecondViewControlleralloc]init];
// 设置代理
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
textField.text= string;
}
- (void)actionButton:(UIButton*)button
{
// 要拿到要传递的数据
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
NSString *string = textField.text;
// 要有一个属性来接收这个数据
SecondViewController *second = [[SecondViewControlleralloc]init];
second.string= string;
// 设置代理
second.delegate= self;
[self.navigationControllerpushViewController:secondanimated:YES];
// 要拿到要传递的数据
UITextField *textField = (UITextField*)[self.viewviewWithTag:888];
NSString *string = textField.text;
// 要有一个属性来接收这个数据
SecondViewController *second = [[SecondViewControlleralloc]init];
second.string= string;
// 设置代理
second.delegate= self;
[self.navigationControllerpushViewController:secondanimated:YES];
}
5.回到后一个界面 顶让代理调用方法进行传值
- (void)actionButton:(UIButton*)button
{
UITextField *textField = (UITextField*)[self.viewviewWithTag:999];
[_delegatechangeTextFieldString:textField.text];
[self.navigationControllerpopToRootViewControllerAnimated:YES];
{
UITextField *textField = (UITextField*)[self.viewviewWithTag:999];
[_delegatechangeTextFieldString:textField.text];
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
6.根拿到传过来的参数进行赋值
*/
三. NSTIMER
//NSTimer
//创建一个定时器 定时循环执行某一个方法 (NSTimeInterval)时间的间隔 如下 1.0 就是每1.0秒执行一次方法 userInfo 可以当成一个参数 也可以标识这个定时器是干什么的
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:@"haha" repeats:YES];
//激活定时器 相当于启动
[timer fire];
//停止
[timer invalidate];
四.NSUserDefaults
// NSUserDefaults 最轻量级的持久化
// 一般只用这个来存储 例如:账号/密码/设备ID... ...
// 单例,目前就叫初始化一个对象就行
NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];
// 保存
[userDefaults setObject:@"尚亚雷"forKey:@"shang"];
// 同步数据
[userDefaults synchronize];
// 打印沙盒路径
NSLog(@"%@",NSHomeDirectory());
/*
/Users/lanou3g/Library/Developer/CoreSimulator/Devices/F4C0035C-BFC9-4B9A-95D1-59CE94B63E35/data/Containers/Data/Application/1F290708-5666-45D8-9222-AFAD1EC03E16
*/
/*
// 取出沙盒里面保存的数据
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *string = [userDefaults objectForKey:@"shang"];
NSLog(@"%@", string);
// 一般只用这个来存储 例如:账号/密码/设备ID... ...
// 单例,目前就叫初始化一个对象就行
NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];
// 保存
[userDefaults setObject:@"尚亚雷"forKey:@"shang"];
// 同步数据
[userDefaults synchronize];
// 打印沙盒路径
NSLog(@"%@",NSHomeDirectory());
/*
/Users/lanou3g/Library/Developer/CoreSimulator/Devices/F4C0035C-BFC9-4B9A-95D1-59CE94B63E35/data/Containers/Data/Application/1F290708-5666-45D8-9222-AFAD1EC03E16
*/
/*
// 取出沙盒里面保存的数据
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *string = [userDefaults objectForKey:@"shang"];
NSLog(@"%@", string);
*/
//取出沙盒里面保存的数据
NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];
NSString *string = [userDefaults objectForKey:@"shang"];
NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];
NSString *string = [userDefaults objectForKey:@"shang"];
NSLog(@"%@", string);
0 0
- UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
- UI基础-UINavigationController、界面通信
- 随意细解:UI -- UINavigationController、界面通信
- UINavigationController 、界面通信
- UI一揽子计划 2 (UITextField UIButton)
- UIday0802:UINavigationController 传值(界面通信)
- UI一揽子计划 13 (UITabBarController 、UITabBar、UITabBarItem 、Block传值)
- UI第八天:UINavigationController、界⾯通信
- iOS编程-------UINavigationController / 界面间通信(传值)
- UI一揽子计划 14 (拖控件 、)
- UI一揽子计划 24 (MVC、通知、)
- UI NSTimer
- UI一揽子计划 17 (image的异步加载、KVO观察者模式、KVO进行豆瓣列表界面图片的异步加载)
- UI - UINavigationController
- ui--UINavigationController
- UI课程(界面通信)
- 爬爬爬之路:UI(八)UINavigationController 界面传值
- UI一揽子计划 5 (Target: Action: 、Protocol Delegate、 UIImageView 、手势识别器)
- linux系统启动tomcat报错
- UI一揽子计划 6 (UIControl、UISegmentedControl、UIImageView插入数组图片、UISlider)
- UI一揽子计划 7 ( UIScrollView、 UIPageControl、综合所学简单相册)
- 以注解方式模拟Spring的IoC,AOP
- Unity-Shader Sublime 自制shader提示插件
- UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
- UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
- 同步与异步访问网络
- 黑马程序员——C语言——C语言中的几种编程思想
- Elasticsearch 分布式安装
- UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
- 个推和极光推送技术介绍
- UI一揽子计划 12 (模态ViewController、单例、通讯录实战、)