UIApplication传值

#import <UIKit/UIKit.h>

@interface VCFirst : UIViewController

@end
#import "VCFirst.h"
#import "VCSecond.h"
#import "AppDelegate.h"

@interface VCFirst ()
{
    AppDelegate* appdele;
    UIButton* btn;
}
@end

@implementation VCFirst

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    btn.frame = CGRectMake(100, 100, 80, 40);
    [btn setTitle:@"换色" forState:UIControlStateNormal] ;
    [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:btn] ;
}
-(void) viewWillAppear:(BOOL)animated{
   
    //获取全局变量UIApplication
    UIApplication* app = [UIApplication sharedApplication] ;
    appdele = app.delegate ;
    
    if (appdele.changeColor == nil) {
        return ;
    }
    if (appdele.str){
        [btn setTitle:appdele.str forState:UIControlStateNormal] ;
    }else{
        [btn setTitle:@"换色" forState:UIControlStateNormal] ;
    }
    //将背景颜色设置为更改后的颜色值
    self.view.backgroundColor = appdele.changeColor ;

}


#pragma mark - Button SEL
-(void) pressBtn
{
    //建立控制器二
    VCSecond* vcS = [[VCSecond alloc] init] ;
    
    vcS.color = [UIColor orangeColor] ;//控制器推送传值
   
    //将视图控制器一的对象赋值给控制器二的成员变量
    vcS.vcFirst = self ;
    
    //显示控制器二
    [self presentViewController:vcS animated:YES completion:nil] ;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
#import <UIKit/UIKit.h>

@class VCFirst ;

@interface VCSecond : UIViewController

//颜色属性
@property (retain,nonatomic) UIColor* color ;
//控制器一的对象
@property (retain,nonatomic) VCFirst* vcFirst ;

@end

#import "VCSecond.h"
#import "VCFirst.h"
#import "AppDelegate.h"
#import "VCThird.h"

@interface VCSecond ()

@end

@implementation VCSecond
@synthesize color = _color ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = _color ;
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    btn.frame = CGRectMake(100, 100, 80, 40);
    [btn setTitle:@"改变颜色" forState:UIControlStateNormal] ;
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal] ;
    [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:btn] ;
    
    
    UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    btn2.frame = CGRectMake(100, 200, 180, 40);
    btn2.backgroundColor = [UIColor purpleColor];
    [btn2 setTitle:@"视图二==切换" forState:UIControlStateNormal] ;
    [btn2 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal] ;
    [btn2 addTarget:self action:@selector(pressBtn2) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:btn2] ;
}


-(void) pressBtn2
{
    VCThird* vcThird = [[VCThird alloc] init] ;
    
    vcThird.vcFirst = self.vcFirst ;
    
    vcThird.view.backgroundColor = [UIColor greenColor] ;
    
    [self presentViewController:vcThird animated:YES completion:nil] ;
}

-(void) pressBtn
{
    //得到全局(整个程序中)惟一的UIApplication对象
    //返回值为惟一的对象指针
    UIApplication* app = [UIApplication sharedApplication] ;
    //[[UIApplication alloc] init] ;
    
    //获取Application代理成员变量对象
    AppDelegate* appDelegate = (AppDelegate*)app.delegate ;
    //更改全局惟一的颜色值
    appDelegate.changeColor = [UIColor grayColor] ;
    appDelegate.str = @"wawawa";
}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //使当前(本身)视图控制器消失
    [self dismissViewControllerAnimated:YES completion:nil] ;
}

@end
#import <UIKit/UIKit.h>

@class VCFirst ;
@interface VCThird : UIViewController

@property (retain,nonatomic) VCFirst* vcFirst ;

@end

#import "VCThird.h"
#import "AppDelegate.h"

@interface VCThird ()

@end

@implementation VCThird

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIApplication * app = [UIApplication sharedApplication] ;
    AppDelegate* appDele = app.delegate ;
    appDele.changeColor = [UIColor blackColor] ;
    [self dismissViewControllerAnimated:YES completion:nil] ;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相关文章
相关标签/搜索