使用SharedApplication进行传值
来源:互联网 发布:java开源商城源码 编辑:程序博客网 时间:2024/05/16 08:38
一般而言,在iOS中页面间传值,常见的方法有四种,
1 使用SharedApplication,定义一个变量来传递.
2 使用文件plist,或者NSUserdefault来传递
3通过一个单例的class来传递
4 通过Delegate来传递。
我先学习一下第一种方法,下面为范例:
(1)AppDelegate.h
#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSString *dname;//定义dname传递帐号的值@property (strong, nonatomic) NSString *dpass;//定义dpass传递密码的值@end
AppDelegate.m
#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //create the window self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[ViewController alloc]init]; [self.window makeKeyAndVisible]; return YES;}@end
(2)ViewController.h
#import <UIKit/UIKit.h>#import "AppDelegate.h"@interface ViewController : UIViewController@end
ViewController.m
#import "ViewController.h"#import "NavViewController.h"UITextField* nameTextField;UITextField *passTextField;@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UILabel* nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 50, 50, 30)]; nameLabel.text = @"账号"; [self.view addSubview:nameLabel]; UILabel* passLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 50, 30)]; passLabel.text = @"密码"; [self.view addSubview:passLabel]; nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 50, 150, 30)]; nameTextField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:nameTextField]; passTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; passTextField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:passTextField]; UIButton* loginBtn = [UIButton buttonWithType:UIButtonTypeSystem]; loginBtn.frame = CGRectMake(60, 180, 72, 30); [loginBtn setTitle:@"登陆" forState:UIControlStateNormal]; [self.view addSubview:loginBtn]; [loginBtn addTarget:self action:@selector(onLogin:) forControlEvents:UIControlEventTouchUpInside]; }- (void) onLogin: (id) sender{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; appDelegate.dname = nameTextField.text; ; appDelegate.dpass = passTextField.text; NavViewController* navCtr = [[NavViewController alloc] init]; [self presentViewController:navCtr animated:YES completion:nil]; }@end
(3) NavViewController.h
#import <UIKit/UIKit.h>#import "AppDelegate.h"@interface NavViewController : UIViewController@end
NavViewController.m
#import "NavViewController.h"UILabel* welcomeLabel;@interface NavViewController ()@end@implementation NavViewController- (void)viewDidLoad { [super viewDidLoad]; AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UILabel* helloLabel = [[UILabel alloc]initWithFrame: CGRectMake(30, 50, 150, 30)]; helloLabel.text = @"欢迎您,"; [self.view addSubview:helloLabel]; UILabel* passLabel = [[UILabel alloc]initWithFrame: CGRectMake(30, 100, 150, 30)]; passLabel.text = @"您的密码是,"; [self.view addSubview:passLabel]; UILabel* welcomeLabel = [[UILabel alloc]initWithFrame:CGRectMake(85, 50, 120, 30)]; welcomeLabel.text = appdelegate.dname; [self.view addSubview: welcomeLabel]; UILabel* pwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 100, 120, 30)]; pwdLabel.text = appdelegate.dpass; [self.view addSubview: pwdLabel]; }@end
效果图如下:
1 0
- 使用SharedApplication进行传值
- iOS - 单例传值 /使用使用SharedApplication.进行传值(二)
- 关于 UIApplication sharedApplication 简单使用
- UIApplication.sharedApplication().openURL的使用(Swift版)
- iOS 键盘中使用[UIApplication sharedApplication].statusBarFrame
- iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用
- OS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS中[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]]的使用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- iOS开发中使用[[UIApplication sharedApplication] openURL:]加载其它应用
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 基于Hadoop的数据仓库Hive 学习指南
- Java访问控制权限
- Cocoa pods的安装和使用
- windows上升级npm
- 【我的printf是不是出bug啦!!】之懵逼函数传递
- 使用SharedApplication进行传值
- Python time模块
- 关于Android方法数量限制的问题
- Log4j.properties配置详解
- SDK里应该下载哪些内容
- linux基础命令,图文说明
- 常用SQL整理 - CTE递归
- NYOJ 710 外星人的供给站
- 剑指offer矩形覆盖问题