使用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
原创粉丝点击