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