ios 界面三级回跳 ——代理回调

本文详细介绍了在iOS应用中如何处理界面三级回跳的问题,特别是从C控制器直接返回A控制器的情况。通过使用代理协议,实现了C控制器在完成操作后通知B控制器dismiss,最终返回到A控制器。代码示例展示了代理的设置和调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.界面跳转对代理的理解

IOS 界面跳转

假设有 controller A 、B、C  :

逻辑结构是:A preset B, B present C ,现在项目需求是由C直接跳转到A。类似的问题是:A为在主页面,B为登录界面,C为三方

登陆界面,当三方登陆界面登陆成功之后,需要由C直接返回到A界面。

解决方案:使用代理(当然block也一样,代理看的更加清晰)

逻辑结构是:C 登陆成功之后,自身dismiss,而且B界面也要dismiss,显示A界面。就要在C执行完事之后通知B,让B也dismiss.

也就是用C的代理调用相应的方法。代理的目的是协议方法不是有自身调用,而是由代理调用。就是这个对象1想回调对象2中的方法,

对象1不能直接调用对象2的方法,这个方法必须有对象1自己调用的时候,就要使用代理,即对象1作为对象2的代理,遵守代理协议,

实现代理方法。

代码:

在B   controller:

- (void)toFacebookTransitionViewController{//推出C controller

    VFacebookTransitionViewController *facebook = [[VFacebookTransitionViewController alloc] init];

    _isGotoFB = YES;

    facebook.delegate = self;//将C的代理设为自身

    [self presentViewController:facebook animated:YES completion:nil];

}

//执行C的代理方法:

- (void)dismissAccountViewController

{

    self.view.hidden = YES;

    [self dismissViewControllerAnimated:NO completion:nil];

}


在C controller 中


@protocol VFacebookTransitionViewControllerDelegate <NSObject>//


- (void)dismissAccountViewController;


@end


typedef void(^accountDismissBlock)();

@interface VFacebookTransitionViewController : VBaseViewController

@property (nonatomic,assign) id<VFacebookTransitionViewControllerDelegate>delegate;

@end


- (void)fbButtonPressed{

    [self showSJHudWithTitle:@"Loading"];

    __weak VFacebookTransitionViewController *weakSelf = self;

    [[VThirdPartyLoginManager sharedManager] loginWithFacebookSuccess:^(NSInteger initScore) {


        if (initScore) {

            [weakSelf showAlertWithScore:initScore];

        }else{

            [weakSelf dismissViewControllerAnimated:NO completion:^{//自身dismiss

//接下来使用代理执行dismiss B

                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(dismissAccountViewController)]) {

                    [weakSelf.delegate dismissAccountViewController];

                }

  

            }];//登录成功之后此时

        }

    } failure:^(NSString *errorMsg) {

        [weakSelf dismissSJHud];

        if (errorMsg) {

            [weakSelf showErrorAlertViewMessage:errorMsg topicTitle:@"Error" AlterViewFinished:^(int buttonIndex) {

            }];

        }

    }];


}


2.

A preset B, B present C ,现在项目需求是由C直接跳转到A。类似的问题是:A为在主页面,B为登录界面,C为三方

登陆界面,当三方登陆界面登陆成功之后,需要由C直接返回到A界面。

这个地方所有dismiss的动画都必须是NO,

[weakSelf dismissViewControllerAnimated:NO completion:^{//自身dismiss

//接下来使用代理执行dismiss B

                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(dismissAccountViewController)]) {

                    [weakSelf.delegate dismissAccountViewController];

               }

}


且都在主线程,所以必须是C  dismiss完成在dismiss B


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值