前面有篇文章是说在uialertview里加入UITableView的,当时因为布局的问题,没有直接加成功,
用了一个很繁琐的方法来达到目的,之后又找了些资料,在网上看到一篇文章是教怎么在uialertview中
加入uitextfield来使用之变成一个密码输入框的,从它的布局上,我发现自己忽略了一个有用的东西,
那就是uialertview的初始化时,可以给它加message,如果我赋给message的值是@"/n/n/n"这样的形式,
那么就可以在uialertview的titile和下面的button如果有的话之间撑出一片空间来放置你想要放的东西,
那么uitableView也可以放在那里,布局的问题就解决了。
话不多说,代码如下:
先上加入uitextfield的代码:
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@“Server Password” message:@”/n/n/n” delegate:self cancelButtonTitle:NSLocalizedString(@“Cancel”,nil) otherButtonTitles:NSLocalizedString(@“OK”,nil), nil]; UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)]; passwordLabel.font = [UIFont systemFontOfSize:16]; passwordLabel.textColor = [UIColor whiteColor]; passwordLabel.backgroundColor = [UIColor clearColor]; passwordLabel.shadowColor = [UIColor blackColor]; passwordLabel.shadowOffset = CGSizeMake(0,-1); passwordLabel.textAlignment = UITextAlignmentCenter; passwordLabel.text = @“Account Name”; [passwordAlert addSubview:passwordLabel]; UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@“passwordfield” ofType:@“png”]]]; passwordImage.frame = CGRectMake(11,79,262,31); [passwordAlert addSubview:passwordImage]; UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)]; passwordField.font = [UIFont systemFontOfSize:18]; passwordField.backgroundColor = [UIColor whiteColor]; passwordField.secureTextEntry = YES; passwordField.keyboardAppearance = UIKeyboardAppearanceAlert; passwordField.delegate = self; [passwordField becomeFirstResponder]; [passwordAlert addSubview:passwordField]; [passwordAlert setTransform:CGAffineTransformMakeTranslation(0,0)]; [passwordAlert show]; [passwordAlert release]; [passwordField release]; [passwordImage release]; [passwordLabel release];
下面是直接加入uitableview的代码:
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Server Password" message:@"/n/n/n/n/n/n/n" delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:NSLocalizedString(@"OK",nil), nil]; UIView *lowestView; int i = 0; while (![[passwordAlert.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) { lowestView = [passwordAlert.subviews objectAtIndex:i]; i++; } UITableView * myTableView = [[UITableView alloc]initWithFrame:CGRectMake(16,lowestView.frame.origin.y+70, 252, 120) style:UITableViewStylePlain]; myTableView.delegate =self; myTableView.dataSource = self; [passwordAlert addSubview:myTableView];
当然UITableView的UITableViewDelegate,UITableViewDataSource两个协议,你要在程序中实现。