码迷,mamicode.com
首页 > 移动开发 > 详细

使用SharedApplication进行传值

时间:2016-05-07 07:08:40      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

  一般而言,在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

效果图如下:

  技术分享

 技术分享


  




使用SharedApplication进行传值

标签:

原文地址:http://blog.csdn.net/panjican/article/details/51332219

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!