UIAlertView中加入UITextField

                      



1. 建立一个新的View Based项目。
你可以随意为其取名,我为其命名为TextFieldInAlert。

2. 代码
在viewcontroller.m或TextFieldInAlert.m(如果你命名为TextFieldInAlert的话),然后找到-(void)viewDidLoad方法。取消其注释加入代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-  (void)viewDidLoad  {

 
    [superviewDidLoad];

 
  UIAlertView  *alert  =  [[UIAlertViewalloc]  initWithTitle:@”EnterName Here” 
 
      message:@”this  gets  covered!”delegate:self 
 
      cancelButtonTitle:@”Dismiss”otherButtonTitles:@”OK!”,  nil];

 
  UITextField  *myTextField  =  [[UITextFieldalloc] 
 
      initWithFrame:CGRectMake(12, 45, 260,25)];

 
    [myTextFieldsetBackgroundColor:[UIColorwhiteColor]];

 
    [alertaddSubview:myTextField];

 
    [alertshow];

 
    [alertrelease];
 
    [myTextFieldrelease];

}

我们做的是调用UIAlertView然后添加一个UITextField。你可能注意到了UIAlertView的message部分,我们为其赋值为“this getscovered!”,如果我们不为其加上这个语句,那么alert的按钮会更靠上而弄乱UITextField。你可以试一下拿掉这一行看看会发生什么。现在UITextField出现在UIAlertView中间了。试一下在UITextField输入点什么。噢,为什么键盘盖住了UIAlertView?有一个简单的修正方法。只需加入两行代码就可修正这个问题。加入代码:

1
2
3
    CGAffineTransformmyTransform  =  CGAffineTransformMakeTra nslation(0,60);

 
    [alertsetTransform:myTransform];

完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    UIAlertView  *alert  =  [[UIAlertViewalloc]  initWithTitle:@”EnterName Here” 
 
      message:@”this  gets  covered!”delegate:self 
 
      cancelButtonTitle:@”Dismiss”otherButtonTitles:@”OK!”,  nil];

 
  UITextField  *myTextField  =  [[UITextFieldalloc]  initWithFrame:CGRectMake(12,45, 260, 25)];

 
  CGAffineTransformmyTransform  =  CGAffineTransformMakeTra nslation(0,60);

 
    [alertsetTransform:myTransform];

 
    [myTextFieldsetBackgroundColor:[UIColorwhiteColor]];

 
    [alertaddSubview:myTextField];

 
    [alertshow];

 
    [alertrelease];

 
    [myTextFieldrelease];
现在你按下“Build andRun”,你会注意到UITextField将在显示略高一点的位置,当你在UITextField输入时,键盘不会再盖住。这就是CGAffineTransform的作用。
CGAffineTransformMake
Returns an affine transformation matrix constructed from values youprovide.

CGAffineTransform CGAffineTransformMake (
 
  CGFloat a,
 
  CGFloat b,
 
  CGFloat c,
 
  CGFloat d,
 
  CGFloat tx,
 
  CGFloat ty
);
Parameters
a
The value at position [1,1] in the matrix.
b
The value at position [1,2] in the matrix.
c
The value at position [2,1] in the matrix.
d
The value at position [2,2] in the matrix.
tx
The value at position [3,1] in the matrix.
ty
The value at position [3,2] in the matrix.
Return Value
A new affine transform matrix constructed from the values youspecify.

Discussion
This function creates a CGAffineTransform structure that representsa new affine transformation matrix, which you can use (and reuse,if you want) to transform a coordinate system. The matrix takes thefollowing form:
|a 
0|
|c 
0|
|tx ty 1| 
第三列 0,0,1 是一直都存在的,返回的时候只返回前两列


































你可能感兴趣的:(c,function,Build,Matrix,transformation,structure)