导航视图(一)

模态框视图

1、利用xib自定义一个新的ViewController ModalPageController;

2、在原有的视图控制器中触发跳转,代码如下:

ModalPageController *modelPageController = [[ModalPageController alloc]initWithNibName:@"ModalPageController" bundle:nil];
    
    modelPageController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    [self presentViewController:modelPageController animated:YES completion:^{
        NSLog(@"modelPageController");
    }];

3、在ModalPageController中返回,代码如下:

[self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Close");
    }];

4、两个  ViewController之间的数据交换:

4.1 利用 NSNotificationCenter

1)在原有的ViewController总定义并设置回调,代码如下:

复制代码
//viewDidLoad里
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doComplete:) name:@"testComplete" object:nil];


//定义回调函数
-(void)doComplete:(NSNotification*)notification{
    NSDictionary *data = [notification userInfo];
    if(nil != data){
        NSString *input = [data objectForKey:@"InputText"];
        NSLog(@"%@", input);
    }
}
复制代码

2)在ModalPageController的返回中,实现通知:

复制代码
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Close");
        
        NSString *input = self.inputText.text;
        
        NSDictionary *dic = [NSDictionary dictionaryWithObject:input forKey:@"InputText"];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"testComplete" object:nil userInfo:dic];
        
        
    }];
复制代码

4.2 利用协议

1)定义一个protocal

#import <Foundation/Foundation.h>

@protocol doBack <NSObject>

-(void)getInputText:(NSString *)input;

@end

2)定义原ViewController遵循并实现该协议,如下:

复制代码
#import <UIKit/UIKit.h>
#import "doBack.h"

@interface ViewController : UIViewController<doBack>

@end

-(void)getInputText:(NSString *)input{
    NSLog(@"%@", input);
}
复制代码

3)、在ModelPageController中定义代理,并在跳转前将该代理指向原ViewController

复制代码
//ModalPageController.h中
@property(nonatomic,assign) NSObject<doBack> *delegate;

//
ModalPageController *modelPageController = [[ModalPageController alloc]initWithNibName:@"ModalPageController" bundle:nil];
    
    modelPageController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    modelPageController.delegate = self; //相当于取得原来ViewController的引用
    
    [self presentViewController:modelPageController animated:YES completion:^{
        NSLog(@"modelPageController");
    }];
复制代码

4)、在ModalPageController需要的地方调用:

[self.delegate getInputText:@"hello Delegate"];
posted @   Fredric_2013  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示