iOS Dev (14) NavigationController基础示例
AppDelegate
.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
分享到:
相关推荐
iOS中present出一个透明导航控制器NavigationController 的方法+毛玻璃效果
自定义的一个navigationcontroller内置了tabbar,方便手势返回时隐藏tabbar(无需hidden)
一个简单的ios中navigationcontroller实现的简单例子 1.在电脑上运行时候要注意修改调试类型为模拟器 2.新版本的xcode请不要忘记调版本,此为4.0版本
效果类似于网易新闻,使用CAAnimation实现push,pop动画,支持全屏右滑pop。
大家好,我叫天明。只需要一个TMNavigationController即可解决你的全屏滑动pop问题,支持有navigationbar 和无navigationbar得界面,兼容底部tabbar,可扩展性强。
主要介绍了iOS swift 总结NavigationController出现问题及解决方法的相关资料,需要的朋友可以参考下
效果参考 App:腾讯新闻、百度音乐等等 GitHub:https://github.com/LeoiOS/LCNavigationController
IOS NavigationController Toolbar示例
IOS应用源码——NavigationController.rar
主要介绍了iOS 开发中 NavigationController经常出现的问题原因分析的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
iOS-7-Custom-ModalViewController-Transitions, 在 iOS 7中,实现新的自定义ModalViewController转换的示例项目 iOS-7-Custom-ModalViewController-Transitions在 iOS 7上实现新定制ModalViewController过渡的示例...
navigationController
1、详细讲解了IOS 的导航控制器UINavigationController.
快速实现navigationController全屏滑动返回的分类, 使用简单
这是在Mac10.7上实现的TabBarController+NavigationController。 这个结构在实际运用中很重要。这里面的代码也很简单,很适合初学者的学习。
主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
本文给大家带来了iOS中navigationController 去掉背景图片、去掉底部线条的核心代码,有需要的朋友可以参考下
TabBarController与NavigationController嵌套的例子: 知识点: ⁃ 使用程序的形式创建TabBarController和NavigationController ⁃ TabBarController和NavigationController的多层嵌套 ⁃ NavigationController...