UI_Button的应用

来源:互联网 发布:知乎live可以搜索吗 编辑:程序博客网 时间:2024/04/27 18:38

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    //--------------------------------------------------------------------------

    RootViewController *rootViewController = [[RootViewController alloc]init];

    self.window.rootViewController = rootViewController;

    

    //--------------------------------------------------------------------------

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}









#import "RootViewController.h"

#import "loginView.h"

#import "LTView.h"

#import "TwoViewController.h"


@interface RootViewController ()<UITextFieldDelegate>


@end


@implementation RootViewController


//记载视图的方法 此方法一般不需要重写,在此方法中可以定制当前视图控制器的视图

-(void)loadView{

    [super loadView];

    //先初始化登录界面

    loginView *loginView1 = [[loginView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //调用属性的getter方法,使登录界面的子视图初始化并加载到登录界面上

    loginView1.userNameLTView.rightTextField.delegate = self;

    loginView1.pwdLTView.rightTextField.delegate = self;

    

    //将登录界面赋给self.view

    self.view = loginView1;

}



#pragma  mark -textField的代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

//回收键盘

    [textField resignFirstResponder];

    return YES;

}



- (void)viewDidLoad {

    [super viewDidLoad];

   

   //添加按钮,实现:点击按钮可以跳转到TwoVC

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    [btn setFrame:CGRectMake(100, 300, 100, 100)];

    [btn setTitle:@"跳转" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(jumpAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

        NSLog(@"登录界面加载完成---%s",__FUNCTION__);

}

-(void)jumpAction:(UIButton *)sender{

    TwoViewController *twoVC = [[TwoViewController alloc]init];

    [self presentViewController:twoVC animated:YES completion:^{

        

    }];

}






//内存警告,系统为每个应用都分配了运行内存,一般情况下为30M;当执行此方法的时候,说明当前应用程序内存已经超出了系统所分配的内存大小。所以要再此方法中进行一些资源的释放。

- (void)didReceiveMemoryWarning {

    

    [super didReceiveMemoryWarning];

    //当内存警告时,将当前界面释放掉

    //首先需要判断当前界面已经加载并且没有显示,这个时候才可以释放掉此界面。

    //判断当前控制器视图是否加载       isViewLoaded

    //如果值为nil说明未显示,如果不为nil说明正在显示

    if ([self isViewLoaded] == YES && self.view.window ==nil) {

        //对象指向nil的时候,只是对原来开辟的空间失去了所有权。也就是告诉系统这块内存现在无人使用,当需要的时候,可以将此内存回收。

        self.view = nil;

   }

    NSLog(@"释放了登录界面---%s",__func__);

}





//是否让当前视图支持屏幕旋转返回值为YES:支持;    NO:不支持

#pragma  mark -屏幕旋转相关代码

-(BOOL)shouldAutorotate{

    return YES;

}


//设置旋转的方向

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;

  

//    UIInterfaceOrientationMaskPortrait; 正常

//    UIInterfaceOrientationLandscapeLeft;向左

//    UIInterfaceOrientationLandscapeRight;向右

//    UIInterfaceOrientationMaskAll;      全部支持

  

    //在枚举值中,如果想要同时支持两个枚举类型,只需要用单或连接两个枚举值即可。

}



@end











#import "TwoViewController.h"


@interface TwoViewController ()


@end


@implementation TwoViewController


- (void)viewDidLoad {

    [super viewDidLoad];

 //--------------------------------------------------------------------------

    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    [backBtn setTitle:@"返回" forState:UIControlStateNormal];

    backBtn.frame = CGRectMake(100, 100, 100, 100);

    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:backBtn];

    NSLog(@"注册界面加载完成---%s",__FUNCTION__);

//--------------------------------------------------------------------------

}

//返回上一界面

-(void)backAction:(UIButton *)sender{

[self dismissViewControllerAnimated:YES completion:^{

    


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

   

    if ([self isViewLoaded] == YES && self.view.window ==nil) {

        self.view = nil;

    }

    NSLog(@"哈哈%s",__func__);

}






#import "loginView.h"

#import "LTView.h"


@implementation loginView


//懒加载   实际上就是对属性的getter方法的重写,所以不需要手动调用此方法,属性的点语法自然会调用自己的get方法。


-(LTView *)userNameLTView{

    //每次调用getter方法的时候,都判断属性是否存在,如果存在,说明已经初始化过了,如果不存在说明没有初始化,需要初始化。

    if (!_userNameLTView) {  //如果对象不存在

        //当对象不存在的时候,就需要初始化对象

        _userNameLTView = [[LTView alloc]initWithFrame:CGRectMake(40, 100, 280, 40)];

        _userNameLTView.leftLabel.text = @"用户名";

        _userNameLTView.rightTextField.placeholder = @"请输入用户名";

        [self addSubview:_userNameLTView];

    }

    return _userNameLTView;

}


-(LTView *)pwdLTView{

    if (!_pwdLTView) {

        _pwdLTView = [[LTView alloc]initWithFrame:CGRectMake(40, 160, 280, 40)];

        _pwdLTView.leftLabel.text = @"密码";

        _pwdLTView.rightTextField.placeholder = @"请输入密码";

        [self addSubview:_pwdLTView];

    }

    return _pwdLTView;

}





//当屏幕旋转的时候,系统会调用此方法重新布局,如果我们对系统的布局不满意,就需要重写此方法,我们自己手动布局

-(void)layoutSubviews{

    [super layoutSubviews];

    //要根据屏幕方向重新布局,需要知道当前的屏幕方向

    //得到当前的屏幕方向

    //UIApplication sharedApplication   得到当前的应用程序

    //statusBarOrientation  状态栏的方向

    NSUInteger orienation = [UIApplication sharedApplication].statusBarOrientation;

    switch (orienation) {

            //向左或者向右作为同一种布局处理,该界面上面的子控件位置居中,其实就是改变控件的X

        case UIInterfaceOrientationLandscapeLeft:

        case UIInterfaceOrientationLandscapeRight:{

            

           

            //首先得到整个屏幕的宽度,根据屏幕宽度让子视图水平居中

            float screenWidth = [UIScreen mainScreen].bounds.size.width;

            CGRect userNameFrame = self.userNameLTView.frame;

            //居中其实就是  (屏幕的宽度-控件的宽度)/2

                //得到控件的宽度

                float subViewWidth =CGRectGetWidth(self.userNameLTView.frame);

                //计算控件新的X

                userNameFrame.origin.x = (screenWidth - subViewWidth)/2;

            self.userNameLTView.frame = userNameFrame;

    }

            

        case UIInterfaceOrientationPortrait:{

            self.userNameLTView.frame = CGRectMake(100, 100, 280, 40);

            self.pwdLTView.frame = CGRectMake(100, 160, 280, 40);

    }

            break;

           

          

         

    

    

        default:

            break;

    }

}










#import "LTView.h"


@implementation LTView


//因为labrlTextField要根据LTView的大小来布局,所以我们需要拿到LTViewframe,所以需要重写LTView的初始化方法。

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

     //在初始化方法中,为LTView添加label textField

        //初始化label。并且添加到LTView  label textField的宽度比例大约为12

           //得到整个屏幕宽度

        float sumWidth = CGRectGetWidth(frame);

           //得到label的宽度

        float labelWidth = (sumWidth-20)/3;

        _leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, labelWidth, CGRectGetHeight(frame))];

        [self addSubview:_leftLabel];

       //初始化textField并添加到LTView

        _rightTextField =[[UITextField alloc]initWithFrame:CGRectMake(labelWidth + 20, 0,labelWidth *2, CGRectGetHeight(frame))];

        [self addSubview:_rightTextField];

    }

        return self;

}




0 0
原创粉丝点击