UI开发----自定义视图和视图控制器(Controller)

//  Created By 郭仔  2015年04月14日21:34:01


一.自定义视图:

根据需求的不同,⾃自定义视图继承的类也有所不同。⼀一般⾃自定义的
视图会继承于UIView。以下是⾃自定义视图的要点: 

1、创建⼀一个UIView⼦子类
2、在类的初始化⽅方法中添加⼦子视图 

3、类的.h⽂文件提供⼀一些接⼝口(⽅方法),便于外界操作⼦子视图  

==================

这里以label-textfield自定义视图为例:

把Label和Textfield封装到LTView中,在⼀一定程度上简化了我们的代 码。
往往我们需要对LTView中的Label或者Textfield进⾏行⼀一定的控制,⽐比 如:设置Label的text,获取Textfield的text,给Textfield指定 delegate,设置textColor等。
为了⽅方便外界操作Label和Textfield,因此我们要为外界提供⼀一些方法。

代码实现:

@interface LTView : UIView

@property(nonatomic,retain)UILabel * lable;
@property(nonatomic,retain)UITextField * textField;
- (instancetype) initWithFrame:(CGRect)frame
                  andLabelText:(NSString *)labelText
                andPlaceholder:(NSString *)placeholder;

@end
// 指定义视图,重写init
- (id)init
{
    self = [super init];
    if (self) {
        //
        self = [self initWithFrame:CGRectZero];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/3, frame.size.height)];
        lable.backgroundColor = [UIColor orangeColor];
        [self addSubview:lable];
        // 把创建的lable赋值为_lable
        self.lable = lable;
        [lable release];
        // =========================
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(frame.size.width/3, 0, frame.size.width*2/3, frame.size.height)];
        textField.backgroundColor = [UIColor yellowColor];
        self.textField = textField;
        [self addSubview:textField];
        [textField release];
        
    }
    return self;
}

- (void)dealloc
{
    [_lable release];
    [_textField release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame andLabelText:(NSString *)labelText andPlaceholder:(NSString *)placeholder
{
    self = [self initWithFrame:frame];
    if (self) {
        self.lable.text = labelText;
        self.lable.font = [UIFont systemFontOfSize:15];
        self.textField.placeholder = placeholder;
    }
    return self;
}
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.lable.frame = CGRectMake(0, 0, frame.size.width/3, frame.size.height);
    self.textField.frame = CGRectMake(frame.size.width/3, 0, frame.size.width*2/3, frame.size.height);
}
LoginView视图调用该自定义视图:

CGRect newFrame = CGRectMake(0, 0, 320, 480);
    self = [super initWithFrame:newFrame];
    if (self) {
        LTView * ltView = [[LTView alloc]initWithFrame:CGRectMake(50, 50, 200, 50) andLabelText:@"用户名" andPlaceholder:@"请输入用户名"];
        [self addSubview:ltView];
        // delegate
        ltView.textField.delegate = self;
        ltView.tag = 100;
        [ltView release];
        // ============
        LTView * ltView2 = [[LTView alloc]initWithFrame:CGRectMake(50, 140, 200, 50) andLabelText:@"密码" andPlaceholder:@"请输入密码"];
        ltView2.textField.secureTextEntry = YES;
        //
        ltView2.textField.delegate = self;
        [self addSubview:ltView2];
        //
        ltView2.tag = 200;
        [ltView2 release];
        // ============
        UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        loginBtn.frame = CGRectMake(50, 230, 60, 50);
        loginBtn.backgroundColor = [UIColor redColor];
        [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
        [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginBtn];
        // =====================
        UIButton * loginBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        loginBtn2.frame = CGRectMake(130, 230, 60, 50);
        loginBtn2.backgroundColor = [UIColor redColor];
        [loginBtn2 setTitle:@"注册" forState:UIControlStateNormal];
        [self addSubview:loginBtn2];
        // ============210 230 100 50
        UIButton * loginBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        loginBtn3.frame = CGRectMake(210, 230, 100, 50);
        loginBtn3.backgroundColor = [UIColor redColor];
        [loginBtn3 setTitle:@"找回密码" forState:UIControlStateNormal];
       // [loginBtn3 addTarget:self action:@selector(searchPsw:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginBtn3];
        
二。视图控制器:

⾃自定义视图类继承UIView。在初始化⽅方法中添加⼦子视图控件。
重写controller的loadView⽅方法。创建⾃自定义视图对象,并指定为controller 的view。
将⼦子视图控件对象设置为⾃自定义视图类的属性,在viewDidLoad⽅方法中进 ⾏行设置:添加action、设置delegate等等。
在controller中添加按钮点击事件实现和代理⽅方法的实现

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor greenColor];
//    UISwitch *aSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(200, 300, 50, 50)];
//    [self.view addSubview:aSwitch];
//    [aSwitch release];
    // ===============================
    // 把LoginView给覆盖了
    thirdViewController * thirdVC = [[thirdViewController alloc]init];
    [self.view addSubview:thirdVC.view];
    // retain
    [self addChildViewController:thirdVC];
    [thirdVC release];
    
    
}
// ========
- (void)loadView
{
    LoginView * logV = [[LoginView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view = logV;
    [logV release];
    
}
三,检测屏幕旋转:

注意视图控制器会⾃自动调整view的⼤大⼩小以适应屏幕旋转,bounds
被修改,触发view的layoutSubviews⽅方法。
view重写layoutSubviews⽅方法,根据设备⽅方向,重新布局。
[UIApplication shareApplication].statusBarOrientation提供设备 当前⽅方向。

controller中:

-(NSUInteger)supportedInterfaceOrientations
{
    // 设置当屏幕旋转时,字体是否跟着旋转,根据按位或运算
    // 里面的方向数值为1,2,4,8....
    // 比如3 = 1 + 2 表示两个方向
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
}
view中:

// 重写
// 当视图需要重绘时,会执行该视图的layoutSubviews方法
- (void)layoutSubviews
{
    // 获取设备的方向
    if([UIApplication sharedApplication].statusBarOrientation ==
       UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
    {
        UIView * v1 = [self viewWithTag:100];
        // 这里需要重写setFrame方法
        v1.frame = CGRectMake(50, 50, 400, 50);
        v1.backgroundColor = [UIColor redColor];
    }
    else
    {
        UIView * v1 = [self viewWithTag:100];
        v1.frame = CGRectMake(50, 50, 200, 50);
        v1.backgroundColor = [UIColor redColor];
    }
}
四,处理内存警告:

控制器能监测内存警告,以便我们避免内存不够引起的crash。 在定义的controller⼦子类中重写didReceiveMemoryWarning⽅方法。 释放暂时不使⽤用的资源。(数据对象、图像)

controller的方法:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    // 当视图已经加载到内存,并没有显示在window上时,将视图置空
    if ([self isViewLoaded] && self.view.window == nil) {
        self.view = nil;
    }
}
害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞 害羞






  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
AngularJS `ui-view` 的使用通常涉及以下几个步骤: 1. 安装 AngularJS 和 AngularUI Router。AngularUI Router 是一个用于 AngularJS 的第三方路由模块,可以实现多视图和嵌套路由等功能。 2. 在 HTML 中定义 `ui-view` 指令,用于展示视图。可以定义一个或多个 `ui-view`,每个指令可以使用一个名字来标识。 3. 在 JavaScript 中配置路由,并指定与视图相关的模板和控制器。通常使用 `$stateProvider` 和 `$urlRouterProvider` 服务来配置路由。 下面是一个简单的示例,演示了如何使用 `ui-view` 展示两个页面: ```html <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="utf-8"> <title>AngularJS UI-Router Demo</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.29/angular-ui-router.min.js"></script> <script> // 创建 AngularJS 应用程序 var app = angular.module('myApp', ['ui.router']); // 配置路由 app.config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('home', { url: '/', templateUrl: 'home.html', controller: 'HomeController' }) .state('about', { url: '/about', templateUrl: 'about.html', controller: 'AboutController' }); $urlRouterProvider.otherwise('/'); }); // 控制器定义 app.controller('HomeController', function($scope) { $scope.message = 'Welcome to the homepage!'; }); app.controller('AboutController', function($scope) { $scope.message = 'Learn more about us!'; }); </script> </head> <body> <h1>AngularJS UI-Router Demo</h1> <nav> <a ui-sref="home">Home</a> <a ui-sref="about">About</a> </nav> <div ui-view></div> </body> </html> ``` 在上面的示例中,我们定义了两个路由:`home` 和 `about`。每个路由都指定了一个与之对应的模板和控制器。在 HTML 中,我们使用 `ui-sref` 指令来指定路由,并使用 `ui-view` 指令来展示视图。在这种情况下,我们只有一个 `ui-view`,因此没有必要给它指定任何名字。 当用户点击导航链接时,将会触发路由,对应的视图将会展示在 `ui-view` 中。在这个示例中,我们在 `HomeController` 和 `AboutController` 中定义了一些文本信息,用于展示在对应的视图中。 注意,使用 `ui-sref` 指令时,需要将路由名称作为参数传递给指令。这个名称应该与路由配置中的名称匹配。 这只是一个简单示例,实际应用中可能还需要更多的路由和视图。掌握了 `ui-view` 的基本使用方法之后,你可以继续学习 AngularUI Router 的更多功能,例如嵌套视图、路由参数等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值