灵心如玉,守一生无惧|

SadicZhou

园龄:3年2个月粉丝:7关注:4

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

使用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
复制代码

 

本文作者:SadicZhou

本文链接:https://www.cnblogs.com/SadicZhou/p/17565125.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   SadicZhou  阅读(379)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 尚好的青春 孙燕姿
  2. 2 孙燕姿
  3. 3 克卜勒 孙燕姿
- 孙燕姿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.