UI_Button的应用

这篇博客主要展示了如何在iOS应用中使用UI_Button实现从登录界面到注册界面的跳转。通过创建UIButton,设置标题和点击事件,详细介绍了RootViewController中UI元素的配置和点击按钮触发的方法,以及在TwoViewController中返回上一界面的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;

}




static const ICON_ATTR_DATA cs_ConciseIconAttr[CONCISE_ICON_MAX] = { { CONCISE_ICON_ADVANCED, 417, 7, 57, 54, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_ADVANCED, UI_BMP_CONCISE_ADVANCED_PRESSED, GUI_BLACK, 0, 56, 56, 22, UI_TEXT_CONCISE_ADVANCED, UI_WindowConciseAdvanced }, { CONCISE_ICON_SAVE, 420, 88, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_SAVE, UI_BMP_CONCISE_SAVE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_FILE_SAVE, UI_WindowConciseSave }, { CONCISE_ICON_DELETE, 420, 144, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_DELETE, UI_BMP_CONCISE_DELETE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_EDIT_DELETE, UI_WindowConciseDelete }, { CONCISE_ICON_GRAY, 420, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_GRAY, UI_BMP_CONCISE_GRAY_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_CONCISE_GRAY, UI_WindowConciseGray }, { CONCISE_ICON_TEXT, 18, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_TEXT, UI_BMP_CONCISE_TEXT_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_TEXT, UI_WindowConciseTextObjCreate }, { CONCISE_ICON_DATE, 100, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_DATE, UI_BMP_CONCISE_DATE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_CONCISE_DATE, UI_WindowConciseDateObjCreate }, { CONCISE_ICON_BARCODE, 182, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_BARCODE, UI_BMP_CONCISE_BARCODE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_BARCODE, UI_WindowConciseBarcodeObjCreate }, { CONCISE_ICON_PICTURE, 264, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_PICTURE, UI_BMP_CONCISE_PICTURE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_PIC, UI_WindowPrintEditPictureCreate }, { CONCISE_ICON_FILE, 346, 200, 50, 28, BUTTON_STATE_NORMAL, UI_BMP_CONCISE_FILE, UI_BMP_CONCISE_FILE_PRESSED, GUI_BLACK, -7, 28, 64, 22, UI_TEXT_PRINT_EDIT_FILE, UI_WindowConciseFileCreate } };
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值