iPhone 对话框与输入框的响应简单界面教程


今天介绍一下iphone中UIButton 与UITextField简单的界面弹出对话框以及按钮的响应 。

项目需求:实现两个按钮 ,两个文本框 点击按钮在文本输入框中显示从那个按钮中点进去的信息。

声明类

  1. //   
  2. //  testViewController.h   
  3. //  test   
  4. //   
  5. //  Created by  宣雨松 on 11-7-5.   
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import <UIKit/UIKit.h>   
  10.   
  11. // 在ViewController中实现UIAlertViewDelegate接口 用来监听弹出框 确定与取消   
  12. @interface testViewController : UIViewController <UIAlertViewDelegate>  
  13. {  
  14.     //定义了一个按钮buttonA   
  15.     IBOutlet UIButton *buttonA;  
  16.     //定义了一个文本框A   
  17.     IBOutlet UITextField *textFieldA;   
  18.     //定义了一个按钮buttonB       
  19.     IBOutlet UIButton *buttonB;      
  20.     //定义了一个文本框B       
  21.     IBOutlet UITextField *textFieldB;   
  22. }      
  23. //声明A按钮被按下的一个方法(IBAction) 相当于(void)   
  24. -(IBAction)bttonAPressed:(id)text;      
  25. //声明B按钮被按下的一个方法   
  26. -(IBAction)bttonBPressed:(id)text;  
  27. //注意这两个方法是用来绑定在空间上 稍后我给大家介绍如何绑定   
  28. @end  

接下来我介绍一下控件与方法的绑定 比如我须要点击按钮A 后调用我自己写的方法 bttonApressed() 我需要点中按钮后 右侧出现视图栏 点中 New Referencing Outlet 拉出一条线拖到 左侧上第一个菱形上后 选 buttonA 表示这个butonA 与代码中声明的buttonA关联上了 然后在点中Touch Up Inside 拉出一条线 依然拖动到左侧第一个菱形上 选择bttonAPressed()方法 这表示点击按钮buttonA后 会调用自己写的方法 bttonAPressed()  简单吧 。 Android 开发的可视化布局却是不如IPHONE开发的布局  J2ME 就更不行啦 哈哈( 怀念以前做J2ME游戏ing...)

实现类

  1. //   
  2. //  testViewController.m   
  3. //  test   
  4. //   
  5. //  Created by  宣雨松 on 11-7-5.   
  6. //  Copyright 2011年 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #import "testViewController.h"   
  10.   
  11. @implementation testViewController  
  12.   
  13. - (void)dealloc  
  14. {  
  15.     [super dealloc];  
  16. }  
  17.   
  18. - (void)didReceiveMemoryWarning  
  19. {  
  20.     // Releases the view if it doesn't have a superview.   
  21.     [super didReceiveMemoryWarning];  
  22.       
  23.     // Release any cached data, images, etc that aren't in use.   
  24. }  
  25.   
  26. #pragma mark - View lifecycle   
  27.   
  28. /* 
  29. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
  30. - (void)viewDidLoad 
  31. { 
  32.     [super viewDidLoad] 
  33. } 
  34. */  
  35. UIAlertView * alertA;  
  36. - (void)bttonAPressed:(id)text  
  37. {  
  38.     //在这里实现了按钮A绑定的方法    
  39.     //这里说一下nil  这个东西就好比java 语言中的 null    
  40.     alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了A弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];  
  41.     //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"];   
  42.     //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text       
  43.     //添加了一个取消按钮   
  44.     [alertA addButtonWithTitle:@"取消"];  
  45.     //将这个UIAlerView 显示出来   
  46.     [alertA show];  
  47.     //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯   
  48.     [alertA release];  
  49.   
  50. }  
  51.   UIAlertView * alertB;  
  52. -(void)bttonBPressed:(id)text  
  53. {  
  54.     //在这里实现了按钮B绑定方法   
  55.     alertB = [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了B弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];  
  56.     [alertB show];  
  57.     [alertB release];  
  58. }  
  59.   
  60. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  61. {  
  62.    //在这里添加对话框按钮响应事件 根据不同窗口判断   
  63.     if(alertView == alertA)  
  64.     {  
  65.         switch (buttonIndex)   
  66.         {  
  67.         case 0:  
  68.             textFieldA.text = @"A窗口中点击确认按钮";  
  69.             break;  
  70.         case 1:  
  71.             textFieldA.text = @"A窗口点击取消按钮";  
  72.         default:  
  73.             break;  
  74.         }  
  75.     }else if (alertView == alertB)  
  76.     {  
  77.         textFieldB.text = @"B窗口点击确定按钮";      
  78.     }  
  79. }  
  80. - (void)viewDidUnload  
  81. {  
  82.     [super viewDidUnload];  
  83.     // Release any retained subviews of the main view.   
  84.     // e.g. self.myOutlet = nil;   
  85. }  
  86.   
  87. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  88. {  
  89.     // Return YES for supported orientations   
  90.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  91. }  
  92.   
  93. @end  

相关内容