LTView的属性和方法
因为要在类外部获取输入框的内容,修改lable的标题,所以将lable和textfield写成属性放在.中,这样在外部可以直接进行修改和设置
@interface LTView : UIView<UITextFieldDelegate>//这里已经将输入框的协议入了
@property(nonatomic,retain)UILabel *myLable;
@property(nonatomic,retain)UITextField *myTextField;
@end
创建两个子视图,一个是lable,一个是textfield
self.myLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
self.myLable.backgroundColor = [UIColor yellowColor];
[self addSubview:self.myLable];
[_myLable release];
self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 20, 100, 40)];
self.myTextField.backgroundColor = [UIColor cyanColor];
self.myTextField.delegate = self;
[_myTextField release];
实现有关输入框点击return回收键盘的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
在dealloc方法中也需要将属性回收
-(void)dealloc{
[_myTextField release];
[_myLable release];
[super dealloc];
}
引入头文件
#import "LTView.h"
创建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.myLable.text = @"姓名"
关于LTView的设置与textfield和lable的属性设置原理相同
AlertView的用法
AlertView在使用前需要签协议
@interface AppDelegate ()<UIAlertViewDelegate>
@property(nonatomic,retain)UIAlertView *alertView;
对AlertView进行基本设置
self.alertView = [[UIAlertView alloc]initWithTitle:@"测试一下" message:@"看看效果" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"恩", nil];
[self.alertView show];
self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
AlertView对应实现的点击方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@" ");
UITextField *first = [self.alertView textFieldAtIndex:0];
NSLog(@"%@",first.text);
}