iOS代码实现:创建按钮,绑定按钮事件,读取控件值
2014-08-29 22:25
381 查看
// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rights reserved. // #import <UIKit/UIKit.h> // 视图控制器对象 @interface HelloWorldViewController : UIViewController @property (nonatomic, retain) IBOutlet UITextField *textField; @end @implementation HelloWorldViewController -(void) loadView { // 创建视图对象 UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor lightGrayColor]; self.view = contentView; // 创建文本框 self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 97.0, 31.0)]; self.textField.borderStyle = UITextBorderStyleRoundedRect; self.textField.keyboardType = UIKeyboardTypeNamePhonePad; self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; [contentView addSubview:self.textField]; // 创建按钮 UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)]; [button setTitle:NSLocalizedString(@"Hello", nil) forState:UIControlStateNormal]; button.center = contentView.center; [contentView addSubview:button]; // 绑定按钮事件 [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; } -(void) buttonClicked:(UIButton *)button { // 方法1:从视图层次里获取文本框中文字 NSLog(@"button clicked..."); UITextField *textField = [self.view subviews][0]; NSLog(@"hello, %@", textField.text); // 方法2:把textField从局部变量变更为类属性 NSLog(@"hello, %@", self.textField.text); //用对话框弹出信息 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Hello" message:self.textField.text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [av show]; } @end // 委托对象 @interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> { IBOutlet UIWindow *window; } @property (nonatomic, retain) UIWindow *window; @property (nonatomic, retain) HelloWorldViewController *viewController; //window 必须声明为属性,声明为局部变量则无法绘制视图,显示为黑屏 //apple 官方文档把viewController也声明为属性了 @end @implementation HelloWorldAppDelegate @synthesize window; @synthesize viewController; -(void) applicationDidFinishLaunching:(UIApplication *)application { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]]; self.viewController = [[HelloWorldViewController alloc]init]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; } @end // 程序入口 int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate"); } }
相关文章推荐
- jsb 实现简单的读取ui,ui里的动画,button按钮事件,执行动画代码
- Delphi 代码创建控件与事件动态绑定
- 章鱼哥—VB.NET 如何实现代码自动生成控件 添加绑定事件
- Android为按钮控件绑定事件的五种实现方式
- asp.net C#动态添加创建生成button按钮控件没有触发执行响应click绑定事件处理
- MFC动态创建控件并响应事件代码实现过程
- iOS纯代码创建按键点击事件与非代码创建按键点击事件的实现
- IOS 代码创建控件,并有处理事件
- iOS开发系列之常用自定义控件开发集—继承UIButton控件代码快速创建按钮开发
- ios使用for循环根据个数居中创建一排按钮的代码实现
- cs代码创建的控件实现属性动画
- JS实现在Repeater控件中创建可隐藏区域的代码
- 关于在c#中创建用户控件后,winform应用程序在调用中无法通过点击用户控件的子控件为其自动添加事件代码的问题
- 使用jquery动态生成的标签,需要在代码当中绑定才可以实现事件的监听
- asp.net中将数据库绑定到DataList控件的实现方法与实例代码
- DataGridView数据绑定控件---------按钮添加和事件触发总结
- Android 绑定按钮事件,并发送短信代码
- 动态创建控件支持事件响应并可保存与读取
- 动态创建的表格单元格中的事件实现代码
- gridview控件自定义绑定,响应模板列按钮事件