首页 > 其他分享 >IOS开发-OC页面跳转传递参数

IOS开发-OC页面跳转传递参数

时间:2023-07-19 11:34:16浏览次数:29  
标签:IOS self OC AppDelegate 跳转 datashare1 import datashare2 view

使用OC进行IOS开发页面跳转传递参数的思路:

1.在AppDelegate.h中定义一个可变词典

2.在AppDelegate.m中初始化该可变词典

3.向字典中添加要传递的参数

4.在目标页面拿到参数

 

1.在AppDelegate.h中定义一个可变词典

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,nonatomic)UIWindow *window;
@property (strong, nonatomic) NSMutableDictionary *RouterData;//路由参数字典

@end

 

2.在AppDelegate.m中初始化该可变词典

//
//  AppDelegate.m
//  viewtest
//
//  Created by 001 on 2023/5/9.
//
#define WYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#import "AppDelegate.h"
#import "datashare1.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _RouterData = [[NSMutableDictionary alloc] initWithCapacity:1];
    

    datashare1 *sharview = [[datashare1 alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:sharview];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    NSLog(@"执行了");
    return YES;
}

#pragma mark - UISceneSession lifecycle


@end

3.在源页面向字典中添加要传递的参数

//
//  datashare1.m
//  blog
//
//  Created by 001 on 2023/7/19.
//

#import "datashare1.h"
#import "AppDelegate.h"
#import "datashare2.h"
@interface datashare1 ()
-(void)goShare2;
@end

@implementation datashare1
-(void)goShare2{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.RouterData setObject:@"我是datashare1传递的参数"forKey:@"data"];
    datashare2 *shareview = [[datashare2 alloc] init];
    [self.navigationController pushViewController:shareview animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"跳转" forState:UIControlStateNormal];
    btn.frame = CGRectMake((self.view.frame.size.width-200)/2, (self.view.frame.size.height-100)/2, 200, 100);
    [btn addTarget:self action:@selector(goShare2) forControlEvents:UIControlEventTouchUpInside];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btn];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

4.在目标页面拿到参数

//
//  datashare2.m
//  blog
//
//  Created by 001 on 2023/7/19.
//

#import "datashare2.h"
#import "AppDelegate.h"
@interface datashare2 ()

@end

@implementation datashare2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *text = [delegate.RouterData objectForKey:@"data"];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-400)/2, (self.view.frame.size.height-200)/2, 400, 200)];
    [self.view addSubview:label];
    label.text = text;
    label.textAlignment = NSTextAlignmentCenter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

标签:IOS,self,OC,AppDelegate,跳转,datashare1,import,datashare2,view
From: https://www.cnblogs.com/SadicZhou/p/17565125.html

相关文章

  • shutdown 命令 新功能 加参数 重启进入BIOS 加参数 重启进入启动选项
    如上图,shutdown/r/fw注释:此命令,运行后,作用:重启进入BIOS......
  • 正则表达式解析StarRocks雾化视图中的血缘关系
    解析SQL中的底表主要目标是获取出StarRocks雾化中的底表和字段备注,之后给字段赋予备注值,存入库表,可以动态生成数据字典,web可以利用该表实现mybatis的动态sql拼接,动态化的excel导出导入,魔板等功能。尝试使用了Jsqlparser解析sql语句,发现遇到部分复杂的子查询内包含unionall情况......
  • docker开启远程访问,并添加TLS证书认证
    默认情况下,docker是无法远程访问的但是有些场景下,是需要远程访问的新建存放证书的目录mkdir/etc/docker/certs.dcd/etc/docker/certs.d生成证书生成ca的私钥opensslgenrsa-aes256-passoutpass:123456-outca-key.pem4096生成CA证书opensslreq-new-x509-pas......
  • 使用C#与Websocket完成简单的聊天功能
    1、背景WebSocket出现之前,Web端为了实现即时通讯,所用的技术都是Ajax轮询(polling)。轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTPrequest,然后由服务器返回最新的数据给客服端的浏览器。这种传统的HTTPrequest的模式带来很明显的缺点–浏览器需要不断的向服务器......
  • [转] C# SuperSocket 手把手教你入门 傻瓜教程 7~8
    7(自定义CommandLineProtocol---命令行协议) 目录一、前言二、创建一个自定义命令行协议的工程项目三、安装SuperSocket和SuperSocket.Engine 1、进入【管理NuGet程序包】2、安装SuperSocket3、安装SuperSocket.Engine四、自定义AppSession类1、工程项目中添加一个自定......
  • [未解决] vue transform-blocks解析源代码报错:Illegal tag name. Use '<' to print '<
    报错内容:[vite]Internalservererror:Illegaltagname.Use'<'toprint'<'.用的是这篇博文的源代码展示方法:如何用vite的vueCustomBlockTransforms(自定义块转换)实现源代码展示使用时突然遇到某一个vue文件添加<demo></demo>标签后报错,但其他vue文件可以正常读取和展示......
  • [转] C# SuperSocket 手把手教你入门 傻瓜教程 5~6
    5(探索自定义AppServer、AppSession,Conmmand,用配置文件App.comfig启动服务器) 目录一、为什么要使用自定义类AppServer、AppSession,Conmmand二、为什么要通过配置文件App.comfig启动服务器三、创建工程项目四、下载SuperSocket动态库1、安装SuperSocket组件2、安装SuperSoc......
  • docker-compose使用踩坑
    之前在cm311-1a机顶盒上刷了armbian,开始把之前n5100x86小主机上的服务往上迁移,之前一直用docker,但是docker-compose没用过,正好部署rustdesk官网有docker-compose的示例,就顺便学习了一下,过程中遇到一些问题,特此记录:1.没有docker-compose命令,只有dockercompose(两个单词......
  • [转] SuperSocket 手把手教你入门 傻瓜教程
    1(服务器单向接收客户端发送数据)  目录一、SuperSocket 简介二、Socket调试工具 SocketTool三、移植C#SuperSocket 简介四、移植五、验证致谢:    我是一名单片机嵌入式软件开发工程师,之前从来没有接触过PC端的编程工作,只是这二年由于工程项目有需求,因此匆......
  • Docker被墙了怎么办?换源处理!
    Docker被墙了怎么办?换源处理!命令行输入以下命令换源,按提示进行操作(推荐南京大学和微软云的源)bash<(curl-sSLhttps://ghproxy.com/https://github.com/SuperManito/LinuxMirrors/raw/main/DockerInstallation.sh)命令行代码来自于镜像中国......