不疯不成魔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Appdelegate中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    //1.创建单视图控制器对象,
    FirstViewController *firstVC = [[FirstViewController alloc] init];
     //2.创建导航控制器,并且知道那个导航控制器的根视图控制器
    //0  64
    UINavigationController *navigatinVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    //3.将 导航控制器 作为window的根视图控制器
    self.window.rootViewController = navigatinVC;
    
    //4.释放
    [firstVC release];
    [navigatinVC release];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

FirstViewController

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "Singleton.h"
//代理传值第四步:服从协议
@interface FirstViewController () <SecondViewControllerdelegate>

@end

/**
 *  导航控制器的工作原理
 导航控制器以栈的形式管理视图控制器,当进入下一界面时,该视图控制器入栈,当返回上一界面时,该视图控制器出栈,入栈前,视图控制器开辟空间,出栈后,视图控制器空间被系统回收,屏幕用于显示的是导航控制器的栈顶元素
 */
@implementation FirstViewController
#pragma mark --second view controller delegate
//代理传值第五步;前一个界面实现协议中的方法
- (void)passValue:(NSString *)data
{
   UILabel * label = (UILabel *)[self.view viewWithTag:200];
    label.text = data;
   //NSString *dd = [[(UILabel *)[self.view viewWithTag:200]] text];
}


#pragma mark -self.view
/*
//将要出现
- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
}
//已经出现
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
//     NSLog(@"superView--%@",self.view.superview);
}
//将要消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   // NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}
//已经消失
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    //NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

*/


/**
 *  界面键传值之一:从前往后传
 葵花宝典:属性传值
 招式一:在后一个界面定义属性(属性的类型,要和传输数据的类型一致)
 招式二:在从前一个界面进入下一个界面之前,将数据传给下一界面.
 */



- (void)viewDidLoad {
     NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    NSLog(@"superView--%@",self.view.superview);
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    //进入下一界面 button
    [self setupButton];
    [self configureNavigationBarCommonPropety];
    //[self customizationNavigationBarContent];
    
    
    
    
    //修改导航条上显示的文字的大小和颜色(针对所有界面统一设置)
//    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
//    self.navigationController.navigationBar.titleTextAttributes = dic;
//    [self setTextField];
//    [self showNextlabel];
}
- (void)setTextField{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 250, 280, 40)];
   // textField.backgroundColor = [UIColor blueColor];
    //设置tag
    textField.tag = 100;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"请输入内容让下一个界面显示";
    [self.view addSubview:textField];
    [textField release];
}
- (void)showNextlabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 310, 280, 40)];
    label.tag = 200;
    label.backgroundColor = [UIColor blueColor];
    label.text = @"显示下一个文本内容";
    [self.view addSubview:label];
    [label release];
}
//单独定制导航条
- (void)customizationNavigationBarContent{
    //1.配置导航条显示的文字
    self.navigationItem.title = @"第一个界面";
    //2.配置导航条的titleView
    UISegmentedControl *segmentCont = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"通话"]];
    
    self.navigationItem.titleView = segmentCont;
    
    [segmentCont release];
    //左侧部分
    //3.配置Left
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(handleLeftBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    [leftBarButtonItem release];
    //配置右侧部分
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleRightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    [rightBarButtonItem release];
}

#pragma mark - handle left bar button action
- (void)handleLeftBarButtonAction:(UIBarButtonItem *)barItem
{
    NSLog(@"点击了左侧!");
}
- (void)handleRightBarButtonAction:(UIBarButtonItem *)barItem
{
    NSLog(@"点击了右侧!");
}
//配置导航条,配置完成之后所有界面的导航条效果一样
- (void)configureNavigationBarCommonPropety{
    //1.修改导航条的颜色
    self.navigationController.navigationBar.backgroundColor = [UIColor grayColor];
    //2.修改导航条的渲染颜色
    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
    //3.关闭导航条的毛玻璃效果
    //self.navigationController.navigationBar.translucent = YES;
    //当关闭导航条毛玻璃效果之后,导航控制器的contentView向下偏移64
    
    //4.影藏导航条
    self.navigationController.navigationBar.hidden = NO;
    //5.设置导航图片
    /**
     *  图片尺寸小于44 或者大于44小于64,图片平铺显示到状态条和导航条
     当图片尺寸等于44时,图片只会显示在导航条
     当图片尺寸等于64时,图片会显示在状态和导航条上
     */
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"32030.png"] forBarMetrics:UIBarMetricsDefault];
}




- (void)setupButton{
    UIButton *btnName =[UIButton buttonWithType:UIButtonTypeSystem];
    btnName.frame = CGRectMake(20, 100, 280, 40);
    [btnName setTitle:@"进入下一界面" forState:UIControlStateNormal];
    btnName.backgroundColor = [UIColor orangeColor];
    [btnName addTarget:self action:@selector(handlePushBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnName];
}


- (void)handlePushBtn:(UIButton *)button
{
    //属性传值从前往后传
    /*
    //1.创建第二个视图控制器对象
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //2.通过导航控制器push到下一个界面(视图控制器自带的 navigationContrller 属性能够获取到当前视图控制器的导航控制器,然后,通过导航控制器进行push)
    //第二个界面接收数据
    secondVC.data = [(UITextField *)[self.view viewWithTag:100] text];
    
    
    
    
    [self.navigationController pushViewController:secondVC animated:YES];
    //3.释放
    [secondVC release];
    */
 
//    //代理传值,从后往前传
//    
//    //1.创建第二个视图控制器对象
//    SecondViewController *secondVC = [[SecondViewController alloc] init];
//    
//    //代理传值  第三步,给后一个界面,指定代理对象
//    secondVC.delegate = self;
//
//    //2.通过导航控制器push到下一个界面(视图控制器自带的 navigationContrller 属性能够获取到当前视图控制器的导航控制器,然后,通过导航控制器进行push)
//    
//    [self.navigationController pushViewController:secondVC animated:YES];
//    //3.释放
//    [secondVC release];
    
    
    
    //单例传值
    
    //1.创建第二个视图控制器对象
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //单例传值第三步
    [Singleton mianSingleton].textFieldData = [(UITextField *)[self.view viewWithTag:100] text];
    //2.通过导航控制器push到下一个界面(视图控制器自带的 navigationContrller 属性能够获取到当前视图控制器的导航控制器,然后,通过导航控制器进行push)
    
    [self.navigationController pushViewController:secondVC animated:YES];
    //3.释放
    [secondVC release];

}

@end

SecondViewController.h中设置代理

#import <UIKit/UIKit.h>

//代理传值第一步:定义协议
@protocol SecondViewControllerdelegate <NSObject>

- (void)passValue:(NSString *)data;

@end

@interface SecondViewController : UIViewController
//属性传值第一步:后一个界面定义属性
@property (nonatomic, copy)NSString *data;
//代理传值 第二步:定义代理属性,存储代理
@property (nonatomic, assign) id<SecondViewControllerdelegate>delegate;
@end

SecondViewController.m

#import "SecondViewController.h"
#import "ThirdViewController.h"
//延展
@interface SecondViewController ()

@end

@implementation SecondViewController

/**
 * 界面切换时,视图的变换过程
 1.从前一个进入到下一个界面
 A push B, 此时 方法的处罚顺序 A - viewWillDisappear ,B - viewWillAppear A- viewDidDisappear, B - viewDidAppear
 2.从后一个界面返回到前一个界面
 B pop A 此时的f方法触发顺序为B - viewWillDisappear ,A - viewWillAppear B- viewDidDisappear, A - viewDidAppear
 */
/*
- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}
*/

/**
 *  界面间传值之二:从后往前传值
 辟邪简谱:代理传值 (代理事前一个界面,协议在后一个界面写)
 后一个界面是委托方,前一个界面是被委托方.
 招式一:在后一个界面定义协议(定义一个用于传值的方法,而且方法必须要有参数,参数必须要与所传数据的类型保持一致)
 招式二:在后一个界面定义代理属性,用来保存代理对象.
 招式三:设置后一个界面的代理 ---- 在前一个界面,进入后一个界面之前,设置前一个界面为后一个代理.
 招式四:前一个界面服从协议
 招式五:前一个界面实现协议中的方法.
 招式六:后一个界面让代理执行协议中的方法(执行方法是,把传输的数据作为方法的参数进行参数进行传递),时机:返回上一界面之前
 */


- (void)viewDidLoad {
     NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    [self setupButton];
    [self returnupButton];
    [self customizationNavigationBarContent];
    [self setTextField];
    [self showNextlabel];
}

- (void)setTextField{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 250, 280, 40)];
    textField.backgroundColor = [UIColor blueColor];
    textField.tag = 200;
    textField.placeholder = @"请输入内容让下一个界面显示";
    [self.view addSubview:textField];
    [textField release];
}
- (void)showNextlabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 310, 280, 40)];
    label.backgroundColor = [UIColor blueColor];
    label.text = self.data;
    [self.view addSubview:label];
    [label release];
}


- (void)customizationNavigationBarContent{
    //1.配置导航条显示的文字
    self.navigationItem.title = @"第二个界面";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupButton{
    UIButton *btnName = [UIButton buttonWithType:UIButtonTypeSystem];
    btnName.frame = CGRectMake(20, 100, 280, 40);
    [btnName setTitle:@"进入下一界面" forState:UIControlStateNormal];
    btnName.backgroundColor = [UIColor orangeColor];
    [btnName addTarget:self action:@selector(handlePushBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnName];
}
- (void)returnupButton{
    UIButton *btnName = [UIButton buttonWithType:UIButtonTypeSystem];
    btnName.frame = CGRectMake(20, 200, 280, 40);
    [btnName setTitle:@"返回一界面" forState:UIControlStateNormal];
    btnName.backgroundColor = [UIColor orangeColor];
    [btnName addTarget:self action:@selector(handlePopBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnName];
}
- (void)handlePushBtn:(UIButton *)button
{
    //1.创建第二个视图控制器对象
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    //2.通过导航控制器push到下一个界面(视图控制器自带的 navigationContrller 属性能够获取到当前视图控制器的导航控制器,然后,通过导航控制器进行push)
    [self.navigationController pushViewController:thirdVC animated:YES];
    //3.释放
    [thirdVC release];
    
}
//返回上一界面
- (void)handlePopBtn:(UIButton *)button
{
    //代理传值第六步:让代理执行任务
    //安全处理,判段代理是否实现了方法,防止崩溃
    if ([self.delegate respondsToSelector:@selector(passValue:)]) {
        [self.delegate passValue:[(UITextField *)[self.view viewWithTag:200] text]];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)dealloc
{
    [_data release];
    [super dealloc];
}
@end

Singleton.h 单例传值

#import <Foundation/Foundation.h>
/**
 *界面间传值之三:多个界面之间传值
 九阴真经:单例传值
 招式1:定义单例类,提供创建单例对象的+号方法.
 2.在单例类中定义属性,存储要传递的数据.(属性数据类型要与传递的数据的类型一致)
 3.在进入下一个界面之前,将数据让单例对象保存
 4.在其他界面中直接从单例中获取数据即可.
 */
@interface Singleton : NSObject
//创建+号方法,提供创建单例对象的方法 (单例命名规范,以main,share,standard,default开头)
//单例类的优势:(1)节省空间 2.共享内存单元

+ (Singleton *)mianSingleton;
@property (nonatomic, copy)NSString *textFieldData;
@end

Singleton.m

@implementation Singleton
//单例的空间在整个程序运行期间都不会回收,会存在内存泄露问题,它的存在是为了解决待定问题
+ (Singleton *)mianSingleton{
    //静态变量
    static Singleton *single = nil;
    if (!single) {
        single = [[Singleton alloc] init];
    }
    return single;
    
}
@end

 

posted on 2015-08-27 21:29  不疯不成魔  阅读(317)  评论(0编辑  收藏  举报