你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 中通過按鈕Button切換背景

IOS 中通過按鈕Button切換背景

編輯:IOS開發綜合

通過按鈕Button觸發事件來切換手機背景有兩種方式,一種建立一個rootViewController作為視圖的根視圖,然後在他的 上面建立兩個新的UIViewController1和UIViewController2通過調用父類的方法

代碼如下:

HHLrootViewController.h

#import 
@class HHLRedViewController;
@class HHLYellowViewController;

@interface HHLrootViewController : UIViewController
@property(retain,nonatomic)HHLRedViewController *pRedVC;
@property(retain,nonatomic)HHLYellowViewController *pYellowVC;

- (IBAction)switchController:(id)sender;

@end

HHLrootViewController.m

#import "HHLrootViewController.h"
#import "HHLRedViewController.h"
#import "HHLYellowViewController.h"

@interface HHLrootViewController ()

@end

@implementation HHLrootViewController

- (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 from its nib.
    HHLRedViewController *redVC=[[HHLRedViewController alloc] initWithNibName:nil bundle: nil];
    self.pRedVC=redVC;
    [redVC release];
    [self.view insertSubview:self.pRedVC.view atIndex:0];
    
    if (self.pRedVC.view.superview==nil)//判斷是否為根視圖
    {
        if (self.pRedVC==nil)//判斷視圖控制器是否初始化
        {
            HHLRedViewController *redVC=[[HHLRedViewController alloc]initWithNibName:@"HHLRedViewController" bundle:nil];
            self.pRedVC=redVC;
            [redVC release];
        }
        [self.pYellowVC.view removeFromSuperview];
        [self.view insertSubview:self.pRedVC.view atIndex:0];
    }
    else{
        if (self.pYellowVC.view.superview==nil) {
            if (self.pYellowVC==nil) {
                HHLYellowViewController *yellowVC=[[HHLYellowViewController alloc]initWithNibName:@"HHLYellowViewController" bundle:nil];
                self.pYellowVC=yellowVC;
                [yellowVC release];
            }
            [self.pRedVC.view removeFromSuperview];
            [self.view insertSubview:self.pYellowVC.view atIndex:0];
        }
   }
    
    
}

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

- (IBAction)switchController:(id)sender {
    if (self.pRedVC.view.superview==nil) {
        if (self.pRedVC==nil) {
            HHLRedViewController *redVC=[[HHLRedViewController alloc]initWithNibName:nil bundle:nil];
            self.pRedVC=redVC;
            [redVC release];
        }
        [self.pYellowVC.view removeFromSuperview];
        [self.view insertSubview:self.pRedVC.view atIndex:0];
    }
    else{
        if (self.pYellowVC.view.superview==nil) {
            if (self.pYellowVC==nil) {
                HHLYellowViewController *yellowVC=[[HHLYellowViewController alloc]initWithNibName:nil bundle:nil];
                self.pYellowVC=yellowVC;
                [yellowVC release];
            }
            [self.pRedVC.view removeFromSuperview];
            [self.view insertSubview:self.pYellowVC.view atIndex:0];
        }
    }
    
}
- (void)dealloc
{
    [_pRedVC release];
    [_pYellowVC release];
    [super dealloc];
}
@end

HHLAppdelegate.h

#import 
#import "HHLrootViewController.h"
@class UIViewController;
@interface HHLAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic) HHLrootViewController *rootView;
@property(strong,nonatomic) UIViewController *redViewController;
@property(strong,nonatomic) UIViewController *yellowViewController;


@end

HHLAppDelegate.m


#import "HHLAppDelegate.h"


@implementation HHLAppDelegate

@synthesize rootView=_rootView;
- (void)dealloc
{
    [_window release];
    [_rootView release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
  HHLrootViewController *rootView1=[[HHLrootViewController alloc]initWithNibName:nil bundle:nil];
    self.rootView = rootView1;
    [rootView1 release];
    self.window.rootViewController=self.rootView;
 //   self.viewController.view.backgroundColor=[UIColor yellowColor];
   
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


HHLRedViewController.h

#import 

@interface HHLRedViewController : UIViewController

@end


HHLRedViewController.m

#import "HHLRedViewController.h"

@interface HHLRedViewController ()

@end

@implementation HHLRedViewController

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

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

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

@end


HHLYellowController.h


#import 

@interface HHLYellowViewController : UIViewController

@end


HHLYellowViewController.m


#import "HHLYellowViewController.h"

@interface HHLYellowViewController ()

@end

@implementation HHLYellowViewController

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

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

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

@end






另一種方法是,建立一個沒有視圖控制器的工程,由於window是UIView的子視圖,所以將window作為根視圖,新建兩個UIViewController1和UIViewController2通過調用父類的方法來進行操作,具體代碼如下:

HHLViewController2.h

#import 

@interface HHLViewController2 : UIViewController

@end

HHLViewController2.m

#import "HHLViewController2.h"

@interface HHLViewController2 ()

@end

@implementation HHLViewController2

- (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 from its nib.
    UILabel *label = [[[UILabel alloc] initWithFrame:self.view.bounds] autorelease];
    label.text=@"你好,世界!";
    //label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"畫面跳轉" forState:UIControlStateNormal];
    [button sizeToFit];
    CGPoint newPoint = self.view.center;
    newPoint.y +=50;
    button.center = newPoint;
    button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [button addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonDidPush{
    [self.view.window sendSubviewToBack:self.view];
}

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

@end

HHLViewController1.h

#import 

@interface HHLViewController1 : UIViewController

@end


HHLViewController1.m

#import "HHLViewController1.h"

@interface HHLViewController1 ()

@end

@implementation HHLViewController1

- (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 from its nib.
    UILabel *label=[[[UILabel alloc] initWithFrame:self.view.bounds] autorelease];
    label.text=@"Hello,world";
    //label.textAlignment=UITextAlignmentCenter;
    label.backgroundColor=[UIColor whiteColor];
    label.textColor=[UIColor blackColor];
    label.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"畫面跳轉" forState:UIControlStateNormal];
    [button sizeToFit];
    CGPoint newPoint=self.view.center;
    newPoint.y +=50;
    button.center =newPoint;
    button.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
    [button addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:button];
    

}
- (void)buttonDidPush{
    [self.view.window sendSubviewToBack:self.view];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


HHLAppDelegate.h

#import 

@class HHLViewController;

@interface HHLAppDelegate : UIResponder 

{
    UIWindow *_window;
    UIViewController *_viewController1;
    UIViewController *_viewController2;
}
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) HHLViewController *viewController;

@end


HHLAppDelegate.m


#import "HHLAppDelegate.h"

#import "HHLViewController1.h"

#import "HHLViewController2.h"

@implementation HHLAppDelegate

- (void)dealloc
{
    [_window release];
    [_viewController1 release];
    [_viewController2 release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    _viewController1=[[HHLViewController1 alloc] init];
    _viewController2=[[HHLViewController2 alloc] init];
    [_window addSubview:_viewController1.view];
    [_window addSubview:_viewController2.view];
    [_window bringSubviewToFront:_viewController1.view];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved