您的位置:首页 > 移动开发

使用SharedApplication进行传值

2016-05-06 16:19 357 查看
一般而言,在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


效果图如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: