您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
创建两个视图控制类MainViewController和SecondViewController
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "MainViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //视图控制器(UIViewController) //抽象类:不能直接使用,需要创建一个子类使用 //创建一个ViewController控制器 MainViewController *mainVC = [[MainViewController alloc] init]; //在程序启动的时候,需要指定一个根视图控制器 self.window.rootViewController = mainVC; [mainVC release]; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (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
MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @end
MainViewController.m
#import "MainViewController.h" #import "SecondViewController.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization NSLog(@"初始化"); //视图控制器的指定初始化方法。。。。。 // 写在这里的代码一定会在初始化的时候执行 // 一般来说,在这个方法中,主要做数据的初始化。 //比如数组/字典等容器的初始化 } return self; } - (void)loadView { [super loadView]; NSLog(@"加载视图"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"视图加载完毕"); //给视图控制器自带的view设置颜色 self.view.backgroundColor = [UIColor whiteColor]; self.view.alpha = 0.5; //button按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(60, 50, 100, 30); [button setTitle:@"" forState:UIControlStateNormal]; button.backgroundColor = [UIColor blueColor]; [button setShowsTouchWhenHighlighted:YES]; button.layer.cornerRadius = 50; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; //button1 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.frame = CGRectMake(150, 50, 100, 30); [button1 setTitle:@"" forState:UIControlStateNormal]; [button1 setShowsTouchWhenHighlighted:YES]; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ]; button1.backgroundColor = [UIColor yellowColor]; button1.layer.cornerRadius = 50; [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //这个方法在视图控制器 自己的view已经被加载完毕的时候调用 //在这个方法中,主要进行视图的铺设。 } - (void)buttonClicked:(UIButton *)button { //初始化 SecondViewController *secondVC = [[SecondViewController alloc] init]; //参数1.要跳转的viewController //参数2.跳转的时候需不需要动画效果 //参数3.block //改变跳转动画 [secondVC setModalTransitionStyle:2]; [self presentViewController:secondVC animated:YES completion:^{ //block中填写, 完成跳转之后,要执行的代码 }]; [secondVC release]; } - (void)viewWillAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"视图将要出现"); }// Called when the view is about to made visible. Default does nothing - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"视图以已经出现"); }// Called when the view has been fully transitioned onto the screen. Default does nothing - (void)viewWillDisappear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"视图将要消失"); }// Called when the view is dismissed, covered or otherwise hidden. Default does nothing - (void)viewDidDisappear:(BOOL)animated { [super viewDidAppear:animated]; NSLog(@"视图已经消失"); }// Called after the view was dismissed, covered or otherwise hidden. Default does nothing - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. //当系统收到内存警告的时候,调用 NSLog(@"内存警告"); } /* #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
SecondViewController.h
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
SecondViewController.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (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 = [UIColor magentaColor]; self.view.alpha = 0.2; //button按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(60, 100, 100, 30); [button setTitle:@"" forState:UIControlStateNormal]; button.backgroundColor = [UIColor blueColor]; [button setShowsTouchWhenHighlighted:YES]; button.layer.cornerRadius = 50; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; //button1 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem]; button1.frame = CGRectMake(150, 100, 100, 30); [button1 setTitle:@"" forState:UIControlStateNormal]; button1.backgroundColor = [UIColor yellowColor]; [button1 setShowsTouchWhenHighlighted:YES]; button1.layer.cornerRadius = 50; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button1 addTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1]; } - (void)buttonClicked:(UIButton *)button { //点击返回 [self dismissViewControllerAnimated:YES completion:^{ }]; } - (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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。