ios关于view-viewcontroller页面之间的跳转的方法

本文总结了在开发过程中常见的页面跳转方式,包括代理跳转、block跳转、通知跳转以及直接在视图跳转。详细解释了每种跳转方式的特点和应用场景,帮助开发者更高效地实现页面间的交互。

在平时编写代码的过程中,页面之间的跳转可以说就和MVC模式一样是开发必须的。但是可能我们知道一种或者几种页面之间的跳转,今天我就来总结一下我在开发之中遇到的所有的页面跳转代码。(关于控制器之间的简单的跳转,比如导航控制器跳转、故事版跳转、简单的模态跳转不在这里多说)

一、代理跳转。

通常我们在跳转中经常是通过你点击了某个事件或者某个操作使你进行控制器之间的跳转。那么我们可以在这个按钮事件或者操作里面写一个代理进行跳转或者传值。

例子:

.h

#import <UIKit/UIKit.h>

@class TestView;

@protocol TestViewDelegate <NSObject>

//跳转控制器

-(void)goToController;


@end

@interface TestView : UIView

//代理

@property(nonatomic,weak)id<TestViewDelegate>delegate;

@end


.m

#import "TestView.h"


@implementation TestView


-(void)layoutSubviews

{

    self.backgroundColor = [UIColor lightGrayColor];

    //创建一个按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:@"跳转" forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btn.frame = CGRectMake(100, 200, 60, 30);

    [btn addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:btn];

}


//按钮事件

-(void)changeView

{

    //代理跳转

    if ([self.delegate respondsToSelector:@selector(goToController)]) {

        [self.delegate goToController];

    }

}

@end


控制器.m

#import "ViewController.h"

#import "TestView.h"

#import "SecViewController.h"

@interface ViewController ()<TestViewDelegate>//声明代理

@property(nonatomic,retain)TestView *testView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //加载视图

    [self loadTestView];

}


-(void)loadTestView

{

    _testView = [[TestView alloc]initWithFrame:self.view.bounds];

    //挂上代理

    _testView.delegate = self;

    [self.view addSubview:_testView];

}


//代理方法实现

-(void)goToController

{

    SecViewController *sec = [[SecViewController alloc]init];

    [self.navigationController pushViewController:sec animated:YES];

}

@end


跳转的控制器.m

#import "SecViewController.h"


@interface SecViewController ()


@end


@implementation SecViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

   //创建按钮方法

   [self loadBtn];

}


-(void)loadBtn

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(100, 300, 60, 30);

    [btn setTitle:@"返回" forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(returnBtn) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

-(void)returnBtn

{

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end



但是这种跳转的不好之处就在于代理书写代码太过繁琐,你不仅需要在视图A里面写一个代理代码你还需要在控制器B里面去实现代理的方法。


二、block跳转。

大家都知道block块的强大之处,我个人认为block是代理的升级版本。他实现了代理能实现的功能,而且代码更加的简洁。只需要在视图A中进行一次声明然后在控制器B 中进行跳转代码的实现。

代码:

.h

#import <UIKit/UIKit.h>

@class TestView;

@protocol TestViewDelegate <NSObject>

//跳转控制器

-(void)goToController;


@end

@interface TestView : UIView

//代理

@property(nonatomic,weak)id<TestViewDelegate>delegate;

//块跳转 

@property(nonatomic,copy)void(^myBlock)();

@end


.m

#import "TestView.h"


@implementation TestView


-(void)layoutSubviews

{

    self.backgroundColor = [UIColor lightGrayColor];

    //创建一个按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:@"跳转" forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btn.frame = CGRectMake(100, 200, 60, 30);

    [btn addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:btn];

}


//按钮事件

-(void)changeView

{

//    //代理跳转

//    if ([self.delegate respondsToSelector:@selector(goToController)]) {

//        [self.delegate goToController];

//    }

    //块跳转

    self.myBlock();

}

@end


控制器.m

#import "ViewController.h"

#import "TestView.h"

#import "SecViewController.h"

@interface ViewController ()<TestViewDelegate>//声明代理

@property(nonatomic,retain)TestView *testView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //加载视图

    [self loadTestView];

}


-(void)loadTestView

{

    _testView = [[TestView alloc]initWithFrame:self.view.bounds];

    //挂上代理

    _testView.delegate = self;

    ViewController *vc = self;

    _testView.myBlock = ^{

        SecViewController *sec = [[SecViewController alloc]init];

        [vc presentViewController:sec animated:YES completion:nil];

        

    };

    [self.view addSubview:_testView];

}


//代理方法实现

-(void)goToController

{

//    SecViewController *sec = [[SecViewController alloc]init];

//    [self presentViewController:sec animated:YES completion:nil];

}

@end


跳转的控制器.m

#import "SecViewController.h"


@interface SecViewController ()


@end


@implementation SecViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

   //创建按钮方法

   [self loadBtn];

}


-(void)loadBtn

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(100, 300, 60, 30);

    [btn setTitle:@"返回" forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(returnBtn) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

-(void)returnBtn

{

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end



但是需要知道的是无论是代理还是block我们都是需要视图和控制器之间存在着联系的,不然没有办法去调用代理或者block。这是大家很容易忽略的地方,也是经常容易错的地方。


三、通知跳转。

我们也可以使用通知进行页面得跳转。通知在我看来就是比代理好的一点就是不需要视图和控制器之间有必然的关联就可以调用(当然他们最大的区别是一个可以多对多传值)。


四、直接在视图跳转

这一个方法是非常的直接,就是在视图中直接的跳转界面。说白了就是调出application然后也是以导航控制器的形式进行跳转。非常的简单实用(当然是在不需要进行传值等操作的时候。)

代码:

视图 .m

#import "TestView.h"

#import "SecViewController.h"

@implementation TestView


-(void)layoutSubviews

{

    self.backgroundColor = [UIColor lightGrayColor];

    //创建一个按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:@"跳转" forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btn.frame = CGRectMake(100, 200, 60, 30);

    [btn addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:btn];

}


//按钮事件

-(void)changeView

{

//    //代理跳转

//    if ([self.delegate respondsToSelector:@selector(goToController)]) {

//        [self.delegate goToController];

//    }

    //块跳转

   // self.myBlock();

    

    

    //视图直接涂转控制器

    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;

    SecViewController *sec = [[SecViewController alloc]init];

    

    [nav pushViewController:sec animated:YES];

}

@end

其他代码和上面一样


### iOS WebView 单页应用实现左滑返回功能 对于在iOS平台上使用WebView加载单页面应用程序(SPA),并希望实现在浏览过程中通过屏幕边缘向右滑动来触发浏览器历史回退的功能,可以利用`WKWebView`及其相关特性。下面提供一种可行的技术路线以及相应的代码片段。 #### 使用 WKWebView 和手势识别器 为了使WebView能够响应用户的左右滑动手势来进行导航操作,可以通过自定义的手势识别逻辑配合JavaScript桥接技术达成目标。具体来说: - 创建一个继承于 `UIViewController` 的类用于管理 Web 页面- 初始化 `WKWebViewConfiguration` 并设置允许 JavaScript 运行; - 加载 HTML 文件至 `WKWebView` 实例中; - 添加一个从左侧边缘开始检测的泛型手势(`UIScreenEdgePanGestureRecognizer`) 到视图控制器上; - 当捕捉到特定方向上的拖拽动作时,检查是否有可退回的历史记录项;如果有,则执行 `goBack()` 方法[^1]。 以下是简化版 Swift 语言下的实现例子: ```swift import UIKit import WebKit class ViewController: UIViewController { var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() let config = WKWebViewConfiguration() webView = WKWebView(frame: self.view.frame, configuration: config) view.addSubview(webView) loadInitialPage() addSwipeGestureRecognizers() } private func loadInitialPage(){ if let url = URL(string:"https://example.com"){ let request = URLRequest(url:url) webView.load(request) } } private func addSwipeGestureRecognizers(){ let edgeRecognizer = UIScreenEdgePanGestureRecognizer(target:self, action:#selector(handleScreenEdgeGesture(_:))) edgeRecognizer.edges = .left view.addGestureRecognizer(edgeRecognizer) } @objc private func handleScreenEdgeGesture(_ gesture:UIScreenEdgePanGestureRecognizer){ switch gesture.state{ case .ended: if webView.canGoBack {webView.goBack()} default: break } } } ``` 此段程序展示了如何在一个简单的iOS App里嵌入一个支持左滑返回前一页行为的Web容器。需要注意的是实际应用场景可能会更加复杂一些,比如处理多级子页面间的跳转、优化用户体验等方面还需要做更多工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值