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
相關文章
相關標籤/搜索