Objective-C Learning Notes - First practice using UITextField and UITextView and UIButton
联合使用文本框,文本视图和按钮创建个简单的故事生成器(伪:
让用户通过3个文本框(UITextField)输入一个名词(地点),一个动词和一个数字.用户还可以输入或修改一个模板,该模板包含将生成的故事概要.由于模板可能多行,因此将使用一个文本视图(UITextView)来显示这些信息.当用户按下按钮(UIButton)时将触发一个操作,该操作将生成故事并将其输入到另一个文本视图中.
我也不知道为什么要贴代码 orz
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //加载背景图片进来 UIImage *viewBackgroundImage = [UIImage imageNamed:@"back.png"]; //设置背景图片 self.view.backgroundColor = [UIColor colorWithPatternImage:viewBackgroundImage]; //加载按钮图片进来 //正常显示的按钮图片 UIImage *normalButtonImage = [[UIImage imageNamed:@"button.png"]stretchableImageWithLeftCapWidth:9.0 topCapHeight:0.0]; //被按下去之后的按钮图片 UIImage *pressedButtonImage = [[UIImage imageNamed:@"button2.png"]stretchableImageWithLeftCapWidth:9.0 topCapHeight:0.0]; //把上面的图片通过 setBackGroundImage 方法加到 view 的按钮中 [self.theButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal]; [self.theButton setBackgroundImage:pressedButtonImage forState:UIControlStateHighlighted]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //这个是根据模板和输入值替换成小故事的函数 - (IBAction)createStory:(id)sender { self.theStory.text = [self.theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:self.thePlace.text]; self.theStory.text = [self.theStory.text stringByReplacingOccurrencesOfString:@"<verb>" withString:self.theVerb.text]; self.theStory.text = [self.theStory.text stringByReplacingOccurrencesOfString:@"<number>" withString:self.theNumber.text]; } //隐藏键盘的函数 - (IBAction)hideKeyBoard:(id)sender { [self.thePlace resignFirstResponder]; [self.theVerb resignFirstResponder]; [self.theNumber resignFirstResponder]; [self.theTemplate resignFirstResponder]; } @end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController //一些 IBOutlet 和 IBAction 罗 @property (strong, nonatomic) IBOutlet UITextField *thePlace; @property (strong, nonatomic) IBOutlet UITextField *theVerb; @property (strong, nonatomic) IBOutlet UITextField *theNumber; @property (strong, nonatomic) IBOutlet UITextView *theTemplate; @property (strong, nonatomic) IBOutlet UITextView *theStory; @property (strong, nonatomic) IBOutlet UIButton *theButton; - (IBAction)createStory:(id)sender; - (IBAction)hideKeyBoard:(id)sender; @end
成功之后的结果大概长这样:)