UI03_LTView(继承UIView)和UIAlertView

来源:互联网 发布:轻而易举软件 编辑:程序博客网 时间:2024/06/04 18:44

前述:分三个部分

       (1)LTView.h文件       (2)LTView.m文件       (3)AppDelegate.m文件

LTView.h文件中

//因为要在类的外部获取输入框的内容,修改label的标题,所以我们可以把这两部分作为属性写在.h文件,这样在外部可以直接进行修改和设置@interface LTView : UIView<UITextFieldDelegate>@property(nonatomic,retain)UILabel *myLabel;@property(nonatomic,retain)UITextField *myTextField;@end

LTView.m文件中

重写init方法既重写默认的初始化方法-(instancetype)initWithFrame:(CGRect)frame{self=[super initWithFrame:frame];//因为继承的是UIView所以由UIView来控制if(self){  [self createView];//自己调用自己 } return self;}-(void)createView{//创建两个子视图,一个是label一个是textFieldself.myLabel=[[UILabel alloc]initWithFrame:CGRectMake(20,20,100,30)];self.myLabel.backgroundColor=[UIColor yellowColor];[self addSubView:self.myLabel];[_myLabel release];self.myTextField[[UITextField alloc]initWithFrame:CGRectMake(150,20,100,40)];self.myTextField.backgroundColor=[UIColor redColor];    [self addSubview:self.myTextField];    //设置代理人    self.myTextField.delegate=self;    [_myTextField release];}实现协议方法-(BOOL)textFieldShouldReturn:(UITextField *)textField{       [textField resignFirstResponder];       return YES;}释放内存-(void)dealloc{    [_myTextField release];    [_myLabel release];    [super dealloc];}

AppDelegate.m文件中

(1)引头文件:#import "LTView.h"(2)让LTView的大小覆盖window   显示出LTView设置的内容    LTView *view=[[LTView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];    [self.window addSubview:view];    [view release];    view.myLabel.text=@"姓名";

UIAlertzView总结(它是点击确认和取消之后就消失了)

1.先签署协议:@interface AppDelegate ()<UIAlertViewDelegate>2.在AppDelegate.m文件中定义一个UIAlertView的属性  @property(nonatomic,retain)UIAlertView *alertView;3.在方法中程序如下:  第一个参数是最上面的标题  第二个参数是标题下的小标题  第三个参数是self  第四个参数是最下面类似形式是取消  第五个参数是确认       self.alertView=[[UIAlertView alloc]initWithTitle:@"测试"message:@"结果"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认",nil];4.让alterView中出现textField   对属性进行设置self.alterView.alertViewStyle=UIAlterViewStyleLoginAndPasswordInput;6.[self.alterView show]//必须写7.设置方法  //先找到alertview中的textfield  //一共两个textfield而0是指第一个textfield下面打印的标题  -(void)alertView:(UIAlertView *)alertView  clickedButtonAtIndex:(NSInteger)buttonIndex{   NSLog(@"11");   UITextField *first=[self.alertView textFieldAtIndex:0];   NSLog(@"%@",first.text);}
0 0