.h
#import <UIKit/UIKit.h>
@class ViewController
@interface AppDelegate: UIResponder <UIApplicationDelegate>
@property <strong, nonatomic> UIWindow *window;
@property <strong, nonatomic> ViewController *vc;
@end
.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.vc]
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController: UIViewController
@end
ViewController.m,注意看 pushMethod 和 presentMethod。注意后者与前者完全不同哦,前者是 self.navigationController 相关的方法,后者是 self 自己的(也就是 UIViewController 的)。注意后者有一个 completion 参数后,如果啥也不想写就用 ^{} 来代替。
#import "ViewController.h"
#import "LLViewController.h"
#import "MMViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *pushButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
[pushButton setTitle:@"Push" forState:UIControlStateNormal];
[pushButton setBackgroundColor:[UIColor redColor]];
[pushButton addTarget:self action:@selector(pushMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:pushButton];
UIButton *presentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
[presentButton setTitle:@"present" forState:UIControlStateNormal];
[presentButton setBackgroundColor:[UIColor redColor]];
[presentButton addTarget:self action:@selector(presentMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:presentButton];
}
- (void)didReceiveMemoryWarning
{
}
- (void)pushMethod:(UIButton *)button
{
[self.navigationController pushViewController:[[LLViewController alloc] init] animated:YES];
}
- (void)presentMethod:(UIButton *)button
{
[self presentViewController:[[MMViewController alloc] init] animated:YES completion:^{}]
}
@end
上面演示的是从主视图切换到另外两个视图,一个是LLViewController,一个是MMViewController。前者是从右向左移入的,后者是从下向上移入的。
LLViewController.h
#import <UIKit/UIKit.h>
@interface LLViewController: UIViewController
@end
LLViewController.m,注意看 popMethod。
#import "LLViewController.h"
@interface LLViewController()
@end
@implementation LLViewController
- (void)didViewLoad
{
[super didViewLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
[button setTitle:@"Pop" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(popMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:button];
}
- (void)didReceiveMemoryWarning
{
}
- (void)popMethod
{
[self.nagivationController popViewControllerAnimated:YES];
}
@end
MMViewController.h
#import <UIKit/UIKit.h>
@interface MMViewController: UIViewController
@end
MMViewController.m 主要看 dismissMethod。注意这个方法与 presentMethod 是类似的,而与 pop 是完全不同的。
#import "MMViewController.h"
@interface MMViewController()
@end
@implementation MMViewController
- (void)didViewLoad
{
[super didViewLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
[button setTitle:@"Dismiss" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(dismissMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:button];
}
- (void)didReceiveMemoryWarning
{
}
- (void)dismissMethod
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end
转载请注明来自:http://blog.csdn.net/prevention