使用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
来源:https://www.cnblogs.com/SadicZhou/p/17565125.html |