先展示效果:
.h文件无需改动。
说明:在“Input Your Name”后的输入框中键入字符,轻点“Click it's to Show now!”,“Output Your Name”后以浅灰色显示出“‘输入的内容’is your Name!
设计方法:将Label、Button、Text Field拖入StoryBoard中,排列成如图所示的,然后输入框拖动到@interface的下方(拖动的时候会有一个指向,箭头到代码区的相应位置会出现一道横杠,松开出现一个属性卡,输入名称即可生成相应的@preperty代码,名称务必唯一),输出的那条Label也按上述方法拖入代码区;然后将Button也按上述方法拖入@implerentation中,会生成一个方法,在生成的代码中的{ … }区间写入代码实现:
self.outputName.text = [NSString stringWithFormat:@"'%@'is your Name!",self.inputName.text];
那么本程序的所有代码如下(其中名称为自己设定的,输入框为inputName,输出框为outputName,按钮为OK):
//
// HWViewController.m
// HelloWorld
//
// Created by Ziwer on 14-3-27.
// Copyright (c) 2014年 FirstProject. All rights reserved.
//
#import "HWViewController.h"
@interface HWViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputName;
@property (weak, nonatomic) IBOutlet UILabel *outputName;
@end
@implementation HWViewController
- (IBAction)OK:(id)sender
{
self.outputName.text = [NSString stringWithFormat:@"'%@'is your Name!",self.inputName.text];
}
@end
.h文件无需改动。