zoukankan      html  css  js  c++  java
  • iOS代码实现:创建按钮,绑定按钮事件,读取控件值

    //
    //  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");
        }
    }
  • 相关阅读:
    前端javascript实现二进制读写操作
    个人关于模块化的理解
    javascript实现与后端相同的枚举Enum对象
    grunt 自定义任务实现js文件的混淆及加密
    熟悉并了解uml的使用(一)
    2021年立个Flag,回顾十多年来的IT职业之路-----没事瞎BB
    使用 Visual Studio 创建 .NET 5 控制台应用程序
    log4net将日志进行分类,保存到不同的目录当中
    .net 使用PowerShell获取电脑中的UUID
    .Net MVC中访问PC网页时,自动切换到移动端对应页面
  • 原文地址:https://www.cnblogs.com/code-style/p/3945823.html
Copyright © 2011-2022 走看看