ios初体验< 运用属性传值,登录>

 注意:ViewController.m文件
// 在第一个页面中,创建一个简单的登录页面,并且添加两个属性
1
#import "ViewController.h" 2 #import "HomeViewController.h" 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIImageView *headerImage;
  // 属性一
5 @property (weak, nonatomic) IBOutlet UITextField *username;
  // 属性二
6 @property (weak, nonatomic) IBOutlet UITextField *password;   // 点击事件
7
- (IBAction)LoginButtonOnClick:(UIButton *)sender; 8 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 19 } 20 21 // 此方法用于切换键盘的控制器 22 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ 23 24 //如果是第一个文本框 取消第一个 并且给第二个 25 if (textField == self.username) { 26 [textField resignFirstResponder]; 27 [self.password becomeFirstResponder]; 28 } 29 30 //如果是第二个 退出响应 31 if (textField == self.password) { 32 [textField resignFirstResponder]; 33 } 34 return YES; 35 } 36 37 // 点击空白部分回收键盘 38 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 39 [self.view endEditing:YES]; 40 } 41 42 - (IBAction)LoginButtonOnClick:(UIButton *)sender { 43 //新建视图 44 HomeViewController *hvcl = [[HomeViewController alloc]init]; 45 //此处新建下一个页面,用属性的点语法,将当前获得文本的赋值给第二个页面里面的属性 (注意接受类型) 46 hvcl.userName = self.username.text; 47 hvcl.passWord = self.password.text;
    //推送到下一页的动画
48 [self presentViewController:hvcl animated:YES completion:nil]; 49 hvcl.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 50 51 } 52 @end

注意:HomeViewController.h 文件  

HomeViewController.h 里面我们新建了 两个外部可以访问的属性代码:当我们新建HomeViewController 的对象时候便可以给他里面属性赋值,这样我们便达到了简单的第一个页面传值到第二个页面

#import <UIKit/UIKit.h> 

  @interface HomeViewController : UIViewController

  //属性一

  @property (nonatomic,copy)NSString *userName;

  //属性二

  @property (nonatomic,copy)NSString *passWord;

  @end



注意:这是HomeViewController.m文件
#import "HomeViewController.h"
@interface HomeViewController ()
- (IBAction)bankButtonOnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 字符串拼接
    _welcomeLabel.text = [NSString stringWithFormat:@"亲爱的%@,欢迎来到%@章节",self.userName,self.passWord];
}
//
- (IBAction)bankButtonOnClick:(UIButton *)sender { //销毁当前视图 返回第一视图 [self dismissViewControllerAnimated:YES completion:nil]; } @end

 完整代码:

#import "ViewController.h"
#import "HomeViewController.h"
//遵守协议
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (nonatomic ,strong)UIActivityIndicatorView *activityIndView;
- (IBAction)LoginButtonOnClick:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置控制器作为文本框的代理代理
    self.username.delegate = self;
    self.password.delegate = self;
    self.headerImage.layer.cornerRadius = 60;
    self.headerImage.layer.masksToBounds = YES;
    NSLog(@"%f %f", _headerImage.frame.size.width, _headerImage.frame.size.height);
    
#pragma make__活动指示器
    // 活动指示器的样式
    _activityIndView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    //位置
    _activityIndView.bounds = CGRectMake(0, 0, 30, 30);
    _activityIndView.center = CGPointMake(self.view.center.x, self.view.center.y);
    
    //动画停止随及隐藏
    _activityIndView.hidesWhenStopped = YES;
    
    [self.view addSubview:_activityIndView];
}

// 搜索这个方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    //如果是第一个文本框 取消第一个 并且给第二个
    if (textField == self.username) {
        [textField resignFirstResponder];
        [self.password becomeFirstResponder];
    }
    
    //如果是第二个 退出响应
    if (textField == self.password) {
        [textField resignFirstResponder];
    }
    return  YES;
}

// 点击空白部分回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (IBAction)LoginButtonOnClick:(UIButton *)sender {
    
    //点击登录按钮  判断文本框类容。
    [_activityIndView startAnimating];
    //新建视图
    HomeViewController *hvcl = [[HomeViewController alloc]init];
    //辅助
    hvcl.userName = self.username.text;
    hvcl.passWord = self.password.text;
    [self presentViewController:hvcl animated:YES completion:nil];
    hvcl.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
}
@end
ViewController.h
#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

@property (nonatomic,copy)NSString *userName;
@property (nonatomic,copy)NSString *passWord;

@end
HomeViewController.h
#import "HomeViewController.h"

@interface HomeViewController ()


- (IBAction)bankButtonOnClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 字符串拼接
    _welcomeLabel.text = [NSString stringWithFormat:@"亲爱的%@,欢迎来到%@章节",self.userName,self.passWord];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)bankButtonOnClick:(UIButton *)sender {
    
    //销毁当前视图 返回第一视图
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end
HomeViewController.m

相关推荐