关于基础视图(以UITextField为例)不响应事件和视图添加后却不出现的几种情况

来源:互联网 发布:偏偏喜欢你淘宝续 编辑:程序博客网 时间:2024/06/05 20:49

1.点击textField没有响应

(1)textField上面还有视图

<pre name="code" class="objc">   UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 220, 40)];    tf.backgroundColor = [UIColor yellowColor];    tf.borderStyle = UITextBorderStyleRoundedRect;    [self.window addSubview:tf];    [tf release];        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];    view.backgroundColor = [UIColor redColor];    [self.window addSubview:view];    [view release];


(2)textField 超出父视图的管理范围

视图能够管理它范围内所有的子视图,如果子视图超出了父视图的范围,就无法响应交互.

<pre name="code" class="objc">    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];    bgView.backgroundColor = [UIColor redColor];    //将自己视图内超出自身管理范围的子视图裁减掉.    bgView.clipsToBounds = YES;    [self.window addSubview:bgView];    [bgView release];        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];    tf.backgroundColor = [UIColor greenColor];    tf.borderStyle = UITextBorderStyleRoundedRect;    [bgView addSubview:tf];    [tf release];


(3)自身问题引起的点击不响应.
 <pre name="code" class="objc">   UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 220, 40)];    tf.backgroundColor = [UIColor greenColor];    //设置能否编辑//    tf.enabled = NO;    //设置是否响应用户交互    tf.userInteractionEnabled = NO;    tf.borderStyle = UITextBorderStyleRoundedRect;    [self.window addSubview:tf];

[tf release];
(4)textField父视图不可交互UILabel默认的用户交互是关闭的
<pre name="code" class="objc">  UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];    view.backgroundColor = [UIColor redColor];    view.userInteractionEnabled = YES;    [self.window addSubview:view];    [view release];        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 40, 320, 40)];    tf.backgroundColor = [UIColor greenColor];    tf.borderStyle = UITextBorderStyleRoundedRect;    [view addSubview:tf];    [tf release];


2.在屏幕上未看到创建的视图

(1)未添加到父视图

   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];    view.backgroundColor = [UIColor redColor];    [self.window addSubview:view];    [view release];
  (2)视图没有设置颜色,颜色依然为clearColor.
  (3)未设置frame
  (4)创建视图方法未调用

(5)添加的父视图为nil

   UIView *view = nil;    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];    view1.backgroundColor = [UIColor redColor];    [view addSubview:view1];    [view1 release];



0 0
原创粉丝点击