使用SharedApplication进行传值
2016-05-06 16:19
357 查看
一般而言,在iOS中页面间传值,常见的方法有四种,
1
使用SharedApplication,定义一个变量来传递.
2
使用文件plist,或者NSUserdefault来传递
3
通过一个单例的class来传递
4
通过Delegate来传递。
我先学习一下第一种方法,下面为范例:
(1)AppDelegate.h
AppDelegate.m
(2)ViewController.h
ViewController.m
(3) NavViewController.h
NavViewController.m
效果图如下:
![](http://img.blog.csdn.net/20160506161650941?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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
效果图如下:
相关文章推荐
- android GridView显示相同尺寸图片以及预览功能
- PropertyPlaceholder与MapperScannerConfigurer同时使用变量无效
- Android自定义视图三:给自定义视图添加“流畅”的动画
- android文件的权限和特殊权限
- Android studio显示行号
- Android自定义视图三:给自定义视图添加“流畅”的动画
- Swift学习第一枪
- android开发:全屏和退出全屏
- 《Android应用性能优化》3——电量、渲染
- 《Android应用性能优化》2——内存、CPU、性能测评
- 《Android应用性能优化》1——代码
- 【转】 Android SDK无法更新解决方法---不错
- iOS 开发技巧
- Android中3种全屏方法及3种去掉标题栏的方法
- android 单元测试
- Android 6.0 运行时权限处理
- 在iOS9上不能分享至微信,提示没有安装微信客户端
- AppCan + 腾讯浏览服务:更快的APP
- 使用PHPExcel 时报错ZipArchive::getFromName(): Invalid or uninitialized Zip object
- android studio 右边没有错误、警告提示怎么办!???