现在的位置: 首页 > 综合 > 正文

iOS中AlertView添加输入框

2013年12月03日 ⁄ 综合 ⁄ 共 6348字 ⁄ 字号 评论关闭

 

 
说明:示范如何利用AlertView来制作系统登入的接口

CustomAlertViewViewController.h

  1. #import <UIKit/UIKit.h>
  2. //记得加入UIAlertViewDelete
  3. @interface CustomAlertViewViewController : UIViewController<UIAlertViewDelegate> {
  4. UIAlertView *myAlertView;
  5. }
  6. @property (nonatomic,retain) UIAlertView *myAlertView;
  7. -(IBAction) buttonPressed:(id)sender;
  8. @end

复制代码

CustomAlertViewViewController.m

  1. -(IBAction) buttonPressed:(id)sender{
  2. myAlertView=[[UIAlertView alloc] initWithTitle:@"系统登入" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登入",nil];
  3. [myAlertView show];
  4. [myAlertView release];
  5. }
  6. - (void)willPresentAlertView:(UIAlertView *)alertView
  7. {
  8. CGRect frame = alertView.frame;
  9. if( alertView==myAlertView )
  10. {
  11. frame.origin.y -= 120;
  12. frame.size.height += 80;
  13. alertView.frame = frame;
  14. for( UIView * view in alertView.subviews )
  15. {
  16. //列举alertView中所有的对象
  17. if( ![view isKindOfClass:[UILabel class]] )
  18. {
  19. //若不UILable则另行处理
  20. if (view.tag==1)
  21. {
  22. //处理第一个按钮,也就是 CancelButton
  23. CGRect btnFrame1 =CGRectMake(30, frame.size.height-65, 105, 40);
  24. view.frame = btnFrame1;
  25. } else if (view.tag==2){
  26. //处理第二个按钮,也就是otherButton
  27. CGRect btnFrame2 =CGRectMake(142, frame.size.height-65, 105, 40);
  28. view.frame = btnFrame2;
  29. }
  30. }
  31. }
  32. //加入自订的label及UITextFiled
  33. UILabel *lblaccountName=[[UILabel alloc] initWithFrame:CGRectMake( 30, 50,60, 30 )];;
  34. lblaccountName.text=@"账号:";
  35. lblaccountName.backgroundColor=[UIColor clearColor];
  36. lblaccountName.textColor=[UIColor whiteColor];
  37. UITextField *accoutName = [[UITextField alloc] initWithFrame: CGRectMake( 85, 50,160, 30 )];
  38. accoutName.placeholder = @"账号名称";
  39. accoutName.borderStyle=UITextBorderStyleRoundedRect;
  40. UILabel *lblaccountPassword=[[UILabel alloc] initWithFrame:CGRectMake( 30, 85,60, 30 )];;
  41. lblaccountPassword.text=@"密码:";
  42. lblaccountPassword.backgroundColor=[UIColor clearColor];
  43. lblaccountPassword.textColor=[UIColor whiteColor];
  44. UITextField *accoutPassword = [[UITextField alloc] initWithFrame: CGRectMake( 85, 85,160, 30 )];
  45. accoutPassword.placeholder = @"登入密码";
  46. accoutPassword.borderStyle=UITextBorderStyleRoundedRect;
  47. //输入的数据以星号显示(密码数据)
  48. accoutPassword.secureTextEntry=YES;
  49. [alertView addSubview:lblaccountName];
  50. [alertView addSubview:accoutName];
  51. [alertView addSubview:lblaccountPassword];
  52. [alertView addSubview:accoutPassword];
  53. }
  54. }
  55. - (void)dealloc {
  56. [myAlertView release];
  57. [super dealloc];
  58. }
复制代码
 
 
说明:示范如何利用AlertView来制作系统登入的接口

CustomAlertViewViewController.h

  1. #import <UIKit/UIKit.h>
  2. //记得加入UIAlertViewDelete
  3. @interface CustomAlertViewViewController : UIViewController<UIAlertViewDelegate> {
  4. UIAlertView *myAlertView;
  5. }
  6. @property (nonatomic,retain) UIAlertView *myAlertView;
  7. -(IBAction) buttonPressed:(id)sender;
  8. @end

复制代码

CustomAlertViewViewController.m

  1. -(IBAction) buttonPressed:(id)sender{
  2. myAlertView=[[UIAlertView alloc] initWithTitle:@"系统登入" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登入",nil];
  3. [myAlertView show];
  4. [myAlertView release];
  5. }
  6. - (void)willPresentAlertView:(UIAlertView *)alertView
  7. {
  8. CGRect frame = alertView.frame;
  9. if( alertView==myAlertView )
  10. {
  11. frame.origin.y -= 120;
  12. frame.size.height += 80;
  13. alertView.frame = frame;
  14. for( UIView * view in alertView.subviews )
  15. {
  16. //列举alertView中所有的对象
  17. if( ![view isKindOfClass:[UILabel class]] )
  18. {
  19. //若不UILable则另行处理
  20. if (view.tag==1)
  21. {
  22. //处理第一个按钮,也就是 CancelButton
  23. CGRect btnFrame1 =CGRectMake(30, frame.size.height-65, 105, 40);
  24. view.frame = btnFrame1;
  25. } else if (view.tag==2){
  26. //处理第二个按钮,也就是otherButton
  27. CGRect btnFrame2 =CGRectMake(142, frame.size.height-65, 105, 40);
  28. view.frame = btnFrame2;
  29. }
  30. }
  31. }
  32. //加入自订的label及UITextFiled
  33. UILabel *lblaccountName=[[UILabel alloc] initWithFrame:CGRectMake( 30, 50,60, 30 )];;
  34. lblaccountName.text=@"账号:";
  35. lblaccountName.backgroundColor=[UIColor clearColor];
  36. lblaccountName.textColor=[UIColor whiteColor];
  37. UITextField *accoutName = [[UITextField alloc] initWithFrame: CGRectMake( 85, 50,160, 30 )];
  38. accoutName.placeholder = @"账号名称";
  39. accoutName.borderStyle=UITextBorderStyleRoundedRect;
  40. UILabel *lblaccountPassword=[[UILabel alloc] initWithFrame:CGRectMake( 30, 85,60, 30 )];;
  41. lblaccountPassword.text=@"密码:";
  42. lblaccountPassword.backgroundColor=[UIColor clearColor];
  43. lblaccountPassword.textColor=[UIColor whiteColor];
  44. UITextField *accoutPassword = [[UITextField alloc] initWithFrame: CGRectMake( 85, 85,160, 30 )];
  45. accoutPassword.placeholder = @"登入密码";
  46. accoutPassword.borderStyle=UITextBorderStyleRoundedRect;
  47. //输入的数据以星号显示(密码数据)
  48. accoutPassword.secureTextEntry=YES;
  49. [alertView addSubview:lblaccountName];
  50. [alertView addSubview:accoutName];
  51. [alertView addSubview:lblaccountPassword];
  52. [alertView addSubview:accoutPassword];
  53. }
  54. }
  55. - (void)dealloc {
  56. [myAlertView release];
  57. [super dealloc];
  58. }

复制代码

 
 

 
 
说明:示范如何利用AlertView来制作系统登入的接口

CustomAlertViewViewController.h

  1. #import <UIKit/UIKit.h>
  2. //记得加入UIAlertViewDelete
  3. @interface CustomAlertViewViewController : UIViewController<UIAlertViewDelegate> {
  4. UIAlertView *myAlertView;
  5. }
  6. @property (nonatomic,retain) UIAlertView *myAlertView;
  7. -(IBAction) buttonPressed:(id)sender;
  8. @end

复制代码

CustomAlertViewViewController.m

  1. -(IBAction) buttonPressed:(id)sender{
  2. myAlertView=[[UIAlertView alloc] initWithTitle:@"系统登入" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登入",nil];
  3. [myAlertView show];
  4. [myAlertView release];
  5. }
  6. - (void)willPresentAlertView:(UIAlertView *)alertView
  7. {
  8. CGRect frame = alertView.frame;
  9. if( alertView==myAlertView )
  10. {
  11. frame.origin.y -= 120;
  12. frame.size.height += 80;
  13. alertView.frame = frame;
  14. for( UIView * view in alertView.subviews )
  15. {
  16. //列举alertView中所有的对象
  17. if( ![view isKindOfClass:[UILabel class]] )
  18. {
  19. //若不UILable则另行处理
  20. if (view.tag==1)
  21. {
  22. //处理第一个按钮,也就是 CancelButton
  23. CGRect btnFrame1 =CGRectMake(30, frame.size.height-65, 105, 40);
  24. view.frame = btnFrame1;
  25. } else if (view.tag==2){
  26. //处理第二个按钮,也就是otherButton
  27. CGRect btnFrame2 =CGRectMake(142, frame.size.height-65, 105, 40);
  28. view.frame = btnFrame2;
  29. }
  30. }
  31. }
  32. //加入自订的label及UITextFiled
  33. UILabel *lblaccountName=[[UILabel alloc] initWithFrame:CGRectMake( 30, 50,60, 30 )];;
  34. lblaccountName.text=@"账号:";
  35. lblaccountName.backgroundColor=[UIColor clearColor];
  36. lblaccountName.textColor=[UIColor whiteColor];
  37. UITextField *accoutName = [[UITextField alloc] initWithFrame: CGRectMake( 85, 50,160, 30 )];
  38. accoutName.placeholder = @"账号名称";
  39. accoutName.borderStyle=UITextBorderStyleRoundedRect;
  40. UILabel *lblaccountPassword=[[UILabel alloc] initWithFrame:CGRectMake( 30, 85,60, 30 )];;
  41. lblaccountPassword.text=@"密码:";
  42. lblaccountPassword.backgroundColor=[UIColor clearColor];
  43. lblaccountPassword.textColor=[UIColor whiteColor];
  44. UITextField *accoutPassword = [[UITextField alloc] initWithFrame: CGRectMake( 85, 85,160, 30 )];
  45. accoutPassword.placeholder = @"登入密码";
  46. accoutPassword.borderStyle=UITextBorderStyleRoundedRect;
  47. //输入的数据以星号显示(密码数据)
  48. accoutPassword.secureTextEntry=YES;
  49. [alertView addSubview:lblaccountName];
  50. [alertView addSubview:accoutName];
  51. [alertView addSubview:lblaccountPassword];
  52. [alertView addSubview:accoutPassword];
  53. }
  54. }
  55. - (void)dealloc {
  56. [myAlertView release];
  57. [super dealloc];
  58. }

复制代码

 

抱歉!评论已关闭.