视图的跳转,ViewController的使用 。。。。。。

创建两个视图控制类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
时间: 2024-08-09 19:53:49

视图的跳转,ViewController的使用 。。。。。。的相关文章

UI 03 UIViewController (视图控制器)的7个方法与视图的跳转

新建一个继承于UIViewController 的类 在AppDelegate.m 中写如下代码. //1.创建一个rootViewController对象 RootViewController *rootVC = [[RootViewController alloc]init]; //2.给window设置根视图控制器 self.window.rootViewController = rootVC; [rootVC release]; 在RootViewController.m文件中的代码如下

UI - 视图控制器跳转另一个视图控制器特效总结

1. 从一个视图控制器跳转另一个视图控制器的方式是可以进行设置的 CATransition *animation = [[CATransition alloc]init]; animation.duration = 1; animation.type = @"pageCurl";  //立方体翻滚效果 animation.subtype = @"fromBottom";  //从底部开始 [self.view.window.layer addAnimation:an

iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法

一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能,有两种方法. 1.通过方法 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion跳转 相

自定义tabbar,点击中间按钮不切换页面,而是让当前的子视图控制器跳转到新页面。(类似新浪微博发布)

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.delegate = self; /************************隐藏tabbar上的黑色线条***************************/ CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.vi

代码视图与StoryBoard.Xib文件视图的跳转

在storyboard中拖拽的控件,当我们使用纯代码进行编写的时候,进行跳转的时候用我们平时用的[self.navigationController popToViewController:Vc animated:YES], 你会发现跳转的后出现的页面只有你用用纯代码写的,而storyboard中的没有,这时候我们使用这个方法还是比较简单不能实现纯代码视图与StoryBoard.Xib视图跳转的问题: 可以常使用以下的方法: UIStoryboard *story = [UIStoryboard

iOS-UIViewController视图控制器跳转界面的几种常用方法

一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能,有两种方法. 1.通过方法 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion跳转 相

多视图控制器跳转方法

1.Modal: 打开:presentViewController 关闭:dismissViewController 2.Push:需搭配NavigationController使用,采用压栈和出栈的方式 打开:pushViewController 关闭:popViewController 3.Segue:以上两种方式都可以,如果在打开目标控制器前需要额外处理,可使用perfromSegueWithIdentifier方法 Identifier在storyboard里设置.

Unity3D与iOS的交互设计&lt;ViewController 的跳转&gt;

原地址:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/28797_12.html Unity3D与iOS的交互设计<ViewController 的跳转>,有需要的朋友可以参考下. 这也是第一次做这样的需求,也是公司的需要呀.做出这个真是一言难尽呀... 小弟主要不是搞iOS开发的,一直以来都是开发Android开发,只是昨天被老大叫过去做一下iOS的二维码扫描.有点iOS基础的我,只好到处搜索了.然后,二维码扫描做好了,遇

IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较

//代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtton点击监控事件方法-(void)event{};视图的跳转就在这个方法中实现(注意:这个button不在视图控制器里面,就算导了相关头文件,也不能跳转,所以只能通过代理方法,或者代码块等,在这个button(在自定义控件空,非视图控制器)方法中调用相关方法,在视图控制器中去实现) 代理实现方法: 在