iPhone教程 对话框与输入框按钮响应界面

移动开发 iOS
本文介绍的是iPhone教程 对话框与输入框按钮响应界面,主要是界面的操作,我们先来看内容。

iPhone教程 对话框与输入框按钮响应界面是本文要介绍的内容,主要来介绍一下iphone中UIButton 与UITextField简单的界面弹出对话框以及按钮的响应 。项目需求:实现两个按钮 ,两个文本框点击按钮在文本输入框中显示从那个按钮中点进去的信息。如图:

iPhone教程 对话框与输入框按钮响应界面

声明类

  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 就更不行了。如图:

iPhone教程 对话框与输入框按钮响应界面

实现类

  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弹出对话框" 
  41. delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];  
  42.     //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"];  
  43.     //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text      
  44.     //添加了一个取消按钮  
  45.     [alertA addButtonWithTitle:@"取消"];  
  46.     //将这个UIAlerView 显示出来  
  47.     [alertA show];  
  48.     //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯  
  49.     [alertA release];  
  50.  
  51. }  
  52.   UIAlertView * alertB;  
  53. -(void)bttonBPressed:(id)text  
  54. {  
  55.     //在这里实现了按钮B绑定方法  
  56.     alertB = [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了B弹出对话框" 
  57. delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];  
  58.     [alertB show];  
  59.     [alertB release];  
  60. }  
  61. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  62. {  
  63.    //在这里添加对话框按钮响应事件 根据不同窗口判断  
  64.     if(alertView == alertA)  
  65.     {  
  66.         switch (buttonIndex)   
  67.         {  
  68.         case 0:  
  69.             textFieldA.text = @"A窗口中点击确认按钮";  
  70.             break;  
  71.         case 1:  
  72.             textFieldA.text = @"A窗口点击取消按钮";  
  73.         default:  
  74.             break;  
  75.         }  
  76.     }else if (alertView == alertB)  
  77.     {  
  78.         textFieldB.text = @"B窗口点击确定按钮";      
  79.     }  
  80. }  
  81. - (void)viewDidUnload  
  82. {  
  83.     [super viewDidUnload];  
  84.     // Release any retained subviews of the main view.  
  85.     // e.g. self.myOutlet = nil;  
  86. }  
  87. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  88. {  
  89.     // Return YES for supported orientations  
  90.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  91. }  
  92. @end 

小结:iPhone教程 对话框与输入框按钮响应界面的内容介绍我那了,希望本文对你有所帮助。

本文转自 http://blog.csdn.net/xys289187120/article/details/6586961--

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-01 11:33:00

Qt 模态 非模态

2010-08-05 10:42:41

Android开发Android高级编程

2010-01-28 16:55:26

Android对话框

2011-07-21 15:50:42

jQuery Mobi页面对话框

2009-12-28 14:32:31

WPF窗体对话框

2009-12-28 13:47:35

WPF对话框

2009-12-11 15:35:50

PHP弹出对话框

2020-09-24 14:06:19

Vue

2023-07-05 08:15:41

按钮PRSHT.H函数

2009-09-03 17:44:22

iPhone程序开发

2011-05-20 16:49:21

VB.NET

2013-12-27 14:10:36

Android开发Android应用Transform

2010-01-11 09:33:32

VB.NET对话框调用

2009-12-29 15:24:48

WPF对话框

2011-11-23 09:47:36

Winform

2012-12-03 10:47:54

WebJQuery控件

2011-06-02 10:37:02

Android 对话框

2010-01-22 16:27:19

VB.NET关于对话框

2011-05-31 10:26:37

Android 对话框

2023-07-19 14:05:37

点赞
收藏

51CTO技术栈公众号