LTView.h
#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);
}