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

时间:2024-09-14 10:36:32
 注意:ViewController.m文件
// 在第一个页面中,创建一个简单的登录页面,并且添加两个属性
1 #import "ViewController.h"
#import "HomeViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
  // 属性一
@property (weak, nonatomic) IBOutlet UITextField *username;
  // 属性二
@property (weak, nonatomic) IBOutlet UITextField *password;
  // 点击事件
7 - (IBAction)LoginButtonOnClick:(UIButton *)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
} // 此方法用于切换键盘的控制器
- (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 {
//新建视图
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
注意: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 = ;
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(, , , );
_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