zoukankan      html  css  js  c++  java
  • FirstApp,iphone开发学习总结4,UITextField的值To UILabel

    需要交互,在TextFieldViewController.h中创建2个变量,并实现UITextFieldDelegate(Return):

    @interface TextFieldViewController : UIViewController<UITextFieldDelegate>{
        UILabel *resultLbl;
        UITextField *textField;
    }
    @property (retain, nonatomic) UILabel *resultLbl;
    @property (retain, nonatomic) UITextField *textField;

    在TextFieldViewController.m中:

    @synthesize resultLbl;
    @synthesize textField;

    init:

    - (id)init {
        self = [super init];
        if (self) {
            [self setTitle:@"文本展示"];
            
            UIImage *img = [UIImage imageNamed:@""];
            [[self tabBarItem] setImage:img];
        }
        return self;
    }

    在- (void)viewDidLoad中创建TextField、Button和一个Label:

    - (void)viewDidLoad
    {
        textField = [[UITextField alloc] init];
        textField.frame = CGRectMake(40.050.0240.030.0);
        textField.textAlignment = UITextAlignmentLeft;
        [textField setBorderStyle:UITextBorderStyleRoundedRect];
        textField.delegate = self;
        
        UIButton *btnOk = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnOk.frame = CGRectMake(180.0100.0100.030.0);
        [btnOk setTitle:@"OK" forState:UIControlStateNormal];
        [btnOk addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        
        resultLbl= [[UILabel alloc] init];
        resultLbl.frame = CGRectMake(110.0150.0100.050.0);
        resultLbl.text = @"0";
        resultLbl.textAlignment = UITextAlignmentCenter;
        resultLbl.font = [UIFont fontWithName:@"" size:10];
        
        [[self view] addSubview:btnOk];
        [[self view] addSubview:textField];
        [[self view] addSubview:resultLbl];
    }

    Button事件:

    - (void)onClick:(id)sender
    {
        if (textField.text) {
            [resultLbl setText:textField.text];
        }else{
            [resultLbl setText:@"NULL"];
        }
    }

    TextField失去焦点:

    - (BOOL)textFieldShouldReturn:(UITextField *)text
    {
        [textField resignFirstResponder];
        return 0;
    }

    释放:

    - (void)dealloc
    {
        [textField release];
        [resultLbl release];
        [super dealloc];
    }

     有更好的方法,请指点,谢谢!

  • 相关阅读:
    开启safe_mode之后对php系统函数的影响
    解析posix与perl标准的正则表达式区别
    教你在不使用框架的情况下也能写出现代化 PHP 代码
    杭州逆行崩溃小伙首度回应
    PHP命令行脚本接收传入参数的三种方式
    PHP魔术方法使用总结
    Nginx服务器的rewrite、全局变量、重定向和防盗链相关功能
    重定向
    P2141 珠心算测验
    T2695 桶哥的问题——吃桶
  • 原文地址:https://www.cnblogs.com/maxfong/p/2481963.html
Copyright © 2011-2022 走看看