UI03_LTView(继承UIView)和UIAlertView

前述:分三个部分

       (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一个是textField
self.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值