LTView&AlertView

LTView的属性和方法

因为要在类外部获取输入框的内容,修改lable的标题,所以将lable和textfield写成属性放在.中,这样在外部可以直接进行修改和设置

@interface LTView : UIView<UITextFieldDelegate>//这里已经将输入框的协议入了

@property(nonatomic,retain)UILabel *myLable;
@property(nonatomic,retain)UITextField *myTextField;
@end

创建两个子视图,一个是lable,一个是textfield

//因为是内部属性,这里只需要用self就可以调用
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];
    //继承原dealloc方法
    [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>
//把AlertView写成属性
@property(nonatomic,retain)UIAlertView *alertView;

对AlertView进行基本设置

self.alertView = [[UIAlertView alloc]initWithTitle:@"测试一下" message:@"看看效果" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"恩", nil];
//initWithTitle: 用来设置提示框的标题
//message: 用来显示提示信息
//delegate: 因为是协议所以要设置代理人

[self.alertView show];//让AlertView显示
//让alertview中出现textfield
self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

AlertView对应实现的点击方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"    ");
//先找到alertview中的textfield
    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、付费专栏及课程。

余额充值