自定义视图
2015-08-08 19:02
281 查看
实现一个功能,不是难事。写出高质量的代码却不是任何程序员都能做到的事。
高质量代码特点:可复用,可移植,精炼等
自定义视图:系统标准UI之外,⼰己组合而出的新的视图。
iOS提供了很多UI组件,借助它们,我们可以做各种程序。
尽管如此,实际开发中,我们还需自定义视图.积累自己的代码库.方便开发.自己封装的视图,能像系统UI控件一样,用于别的项目中,能大大降低开发成本,提高开发效率
自定义视图步骤
根据需求的不同,自定义视图继承的类也有所不同。一般自定义的视图会继承于UIView。以下是自定义视图的要点:
1、创建一个UIView⼦子类
2、在类的初始化方法中添加子视图
3、类的.h文件提供一些接口(方法),便于外界操作子视图
高质量代码特点:可复用,可移植,精炼等
自定义视图:系统标准UI之外,⼰己组合而出的新的视图。
iOS提供了很多UI组件,借助它们,我们可以做各种程序。
尽管如此,实际开发中,我们还需自定义视图.积累自己的代码库.方便开发.自己封装的视图,能像系统UI控件一样,用于别的项目中,能大大降低开发成本,提高开发效率
自定义视图步骤
根据需求的不同,自定义视图继承的类也有所不同。一般自定义的视图会继承于UIView。以下是自定义视图的要点:
1、创建一个UIView⼦子类
2、在类的初始化方法中添加子视图
3、类的.h文件提供一些接口(方法),便于外界操作子视图
#import "AppDelegate.h" #import "LTView.h" // 可以在.m文件中引头文件,签协议 @interface AppDelegate ()<UIAlertViewDelegate> @property(nonatomic, retain)UIAlertView *alertView; @end @implementation AppDelegate - (void)dealloc { [self.window release]; [self.alertView release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [self.window release]; // alert 提示 提醒 警告 注意 self.alertView.delegate = self; self.alertView = [[UIAlertView alloc] initWithTitle:@"皇城PK" message:@"怒刷装备" delegate:self cancelButtonTitle:@"注册即送" otherButtonTitles:@"残忍拒绝", nil]; [self.alertView show]; // 让alertView中出现textField // self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; // Login and Password self.alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; // Password // self.alertView.alertViewStyle = UIAlertViewStylePlainTextInput; // Space LTView *view = [[LTView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)]; [self.window addSubview:view]; [view release]; return YES; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"我打~"); // 先找到alertView中的textField UITextField *first = [self.alertView textFieldAtIndex:0]; NSLog(@"%@", first.text); } @end
#import <UIKit/UIKit.h> @interface LTView : UIView<UITextFieldDelegate> // 因为要在类的外面获取输入框的内容并修改Label的标题,所以把这两部分属性写在.h文件中 @property(nonatomic, retain)UILabel *MyLaber; @property(nonatomic, retain)UITextField *MyTextField; @end
#import "LTView.h" @implementation LTView // 重写默认的初始化方法 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 模块化 [self createView1]; [self createView2]; } return self; } - (void)createView1 { // 创建两个子视图,一个是label,一个是textField self.MyLaber = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 40)]; self.MyLaber.backgroundColor = [UIColor orangeColor]; self.MyTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 100, 180, 40)]; self.MyTextField.backgroundColor = [UIColor cyanColor]; self.MyTextField.delegate = self; [self addSubview:self.MyLaber]; [self addSubview:self.MyTextField]; [self.MyLaber release]; [self.MyTextField release]; self.MyLaber.text = @"用户名"; self.MyTextField.placeholder = @"请输入您的用户名"; } - (void)createView2 { self.MyLaber = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 100, 40)]; self.MyLaber.backgroundColor = [UIColor yellowColor]; [self addSubview:self.MyLaber]; [self.MyLaber release]; self.MyTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 160, 180, 40)]; self.MyTextField.backgroundColor = [UIColor blueColor]; [self addSubview:self.MyTextField]; [self.MyTextField release]; self.MyLaber.text = @"密码"; self.MyTextField.placeholder = @"请输入您的密码"; } - (void)dealloc { [self.MyTextField release]; [self.MyLaber release]; [super dealloc]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end
相关文章推荐
- Improving the GPA 贪心 HDU4968
- 第一次编写opengl程序,遇到“无法打开包括文件:“gl\glaux.h”: No such file or directory”怎么办?
- poj 1276 Cash Machine 多重背包
- poj 1276 Cash Machine 多重背包
- minecraft花生壳怎样开服
- “TCP:三次握手”分析——以一个简单的“服务器”和“客户端”为例
- Reverse Integer
- @Controller's and AOP Proxying
- 小鱼收纳app V1.0.6 安卓版
- 音频播放
- Bootstrap 准备工作
- 首次使用pod遇到问题后在stackoverflow寻找的解决方案
- HDU 4095 Y (树上计数问题)
- HDU 3172 Virtual Friends
- boostrape 栅栏总结
- redhat6.4 安装oracle 10g error
- 基于arm和ds18b20的系统
- CentOS操作系统基于Cmake编译安装MySQL
- fstream读写二进制文件的方法
- JS - 侧边导航收缩伸展