//
// SubViewController.h
// UI2_视图切换
//
// Created by zhangxueming on 15/7/3.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SubViewController : UIViewController
@end
//
// SubViewController.m
// UI2_视图切换
//
// Created by zhangxueming on 15/7/3.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "SubViewController.h"
@interface SubViewController ()
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 300, self.view.frame.size.width-40, 50);
btn.backgroundColor = [UIColor whiteColor];
[btn setTitle:@"视图切换" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClicked
{
NSLog(@"-----");
if (self.view.superview) {
[self.view removeFromSuperview];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// RootViewController.h
// UI2_视图切换
//
// Created by zhangxueming on 15/7/3.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
//
// RootViewController.m
// UI2_视图切换
//
// Created by zhangxueming on 15/7/3.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "RootViewController.h"
#import "SubViewController.h"
@interface RootViewController ()
{
SubViewController *_svc;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
_svc = [[SubViewController alloc] init];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(20, 200, self.view.frame.size.width-40, 50);
[btn setTitle:@"视图切换" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnClicked
{
[self.view addSubview:_svc.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// AppDelegate.m
// UI2_视图切换
//
// Created by zhangxueming on 15/7/3.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
RootViewController *rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = rootViewController;
return YES;
}
转载于:https://www.cnblogs.com/0515offer/p/4638603.html