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)];


//实现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);
  
   
   
}

 //返回上一个界面
    [
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.
要传递的是什么?传递的是数据不是控件
     */
}// 要拿到要传递的数据
   
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;
@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;
}
- (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];
}
 5.回到后一个界面 顶让代理调用方法进行传值
- (void)actionButton:(UIButton*)button
{
   
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);
     */


//取出沙盒里面保存的数据
   
NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];
   
NSString *string = [userDefaults objectForKey:@"shang"];
   NSLog(@"%@", string);
0 0
原创粉丝点击