LTView
2015-08-12 19:44
323 查看
LTView.h
LTView.m
AppDelegate.m
#import <UIKit/UIKit.h> @interface LTVIew : UIView<UITextFieldDelegate> // 因为要在类的外部获取输入框的内容,修改label的标题,所以我们可以把这两部分作为属性写在.h文件中,这样在外部可以直接进行修改和设置 @property(nonatomic,retain)UILabel *myLabel; @property(nonatomic,retain)UITextField *myTextField; @end
LTView.m
#import "LTVIew.h" @implementation LTVIew // 重写默认的初始化方法 -(instancetype)initWithFrame:(CGRect)frame { self=[super initWithFrame:frame];// 重写父类的方法的时候,父类的方法也要继承 if (self) { // 在代码区写额外的事就可以了 // 模块化 // 自己调用自己的方法 [self createView]; } return self; } // 无返回值无参数的方法 -(void)createView { // 创建两个子视图 // 一个是label,一个是textfield self.myLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 30)]; self.myLabel.backgroundColor=[UIColor lightGrayColor]; [self addSubview:self.myLabel]; [_myLabel release]; self.myTextField=[[UITextField alloc]initWithFrame:CGRectMake(150, 20, 100, 30)]; self.myTextField.backgroundColor=[UIColor cyanColor ]; [self addSubview:self.myTextField]; // 设置代理人 self.myTextField.delegate=self; [_myTextField release]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return self; } // 释放 -(void)dealloc { [_myTextField release]; [_myLabel release]; [super dealloc]; } @end
AppDelegate.m
#import "AppDelegate.h" #import "text.h" #import "LTVIew.h" @interface AppDelegate ()<UIAlertViewDelegate> @property(nonatomic,retain)UIAlertView *alertView; @end @implementation AppDelegate -(void)dealloc { [_alertView release]; [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; [_window release]; self.alertView=[[UIAlertView alloc]initWithTitle:@"测试" message:@"效果" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil]; [self.alertView show]; 让alertView中出现textfield // 这个方法可以在alertView中创建出两个textfield,一个Login,一个Password self.alertView.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput; // 创建一个LTView的对象 LTVIew *view=[[LTVIew alloc]initWithFrame:CGRectMake(0, 100, self.window.frame.size.width, self.window.frame.size.height)]; [self.window addSubview:view]; [view release]; // 通过view进行修改 view.myLabel.text=@"姓名"; view.myTextField.placeholder=@"请输入姓名"; return YES; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"dayin"); // 先找到alertView中的textfield // alertviewstyle中的对应的方法会创建出两个textfield,打印的是第一个textfield中输入的内容 UITextField *first=[self.alertView textFieldAtIndex:0]; NSLog(@"%@",first.text); }
相关文章推荐
- C#使用lock锁定静态变量
- HTTPConnection与HTTPClient
- bfs 马的移动
- Telerk-asp.net 第三方控件之控件讲解(一)
- 长按弹出快捷菜单
- http://blog.sina.com.cn/s/blog_777a9ccb01013maj.html
- 【 D3.js 入门系列 --- 9.6 】 生产的包图
- 计算几何模板
- Codeforces Round #Pi (Div. 2) F. Mausoleum DP
- kd树
- hdu 3342 Legal or Not(拓扑排序)
- WCF重写ServiceHost,实现独立配置文件
- FPGA奇数分频器
- Java判断和检查网络
- 万能适配,百分比布局,支持padding
- 九度oj 1014
- Ubuntu12.04 环境搭建
- <%%>创建内联代码块(表达)
- 10gocm->session5->数据库管理实验->GC资源管理器的资源消耗组介绍
- 01背包 动态规划