您的位置:首页 > 产品设计 > UI/UE

我的iphone开发学习笔记(二)使用UITextView, UITextField 和UIButton控件

2011-11-30 22:53 741 查看
刚刚学习了一个实例,关于如何使用UITextField, UIButton的高级控件。

具体练习步骤如下:

1。 创建基于 view_base_application

       命名为 FieldButtonFunApp

2.    打开FieldButtonFunViewController.h

       依次添加需要的控件,并且@property  各个控件, 并且添加两个方法

        

//
//  FielduttonFunViewController.h
//  FielduttonFun
//
//  Created by 旭 陈 on 11-12-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FielduttonFunViewController : UIViewController {

IBOutlet UITextField *thePlace;
IBOutlet UITextField *theVerb;
IBOutlet UITextField *theNumber;
IBOutlet UITextView *theStory;
IBOutlet UITextView *theTemplate;
IBOutlet UIButton *generateStory;
}

@property(retain,nonatomic) UITextField *thePlace;
@property(retain,nonatomic) UITextField *theVerb;
@property(retain,nonatomic) UITextField *theNumber;
@property(retain,nonatomic) UITextView *theStory;
@property(retain,nonatomic) UITextView *theTemplate;
@property(retain,nonatomic) UIButton *generateStory;
-(IBAction)createStory:(id)sender;
-(IBAction)hideKeyboard:(id)sender;

@end


3。 打开FieldFunButtonViewController.xib

       添加需要的控件



5. 链接各个控件与file's owner

    


     点击file's owner, 按住control,拖动到具体的控件,然后从列表中选择控件

      依次完成

      函数的链接方式相反,选中RoundRectButton,按住control到file‘s owner

      看下图

       


6。完成FieldButtonFunViewController.m 中的函数

      -(IBAction)createStory:(id)sender{

          主要的代码就是提取指定字符,切换成相关的文字,看下代码,比较简单的;

    } 

//
//  FielduttonFunViewController.m
//  FielduttonFun
//
//  Created by 旭 陈 on 11-12-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FielduttonFunViewController.h"

@implementation FielduttonFunViewController

@synthesize thePlace;
@synthesize theVerb;
@synthesize theNumber;
@synthesize theStory;
@synthesize theTemplate;
@synthesize generateStory;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)dealloc {
[super dealloc];
[thePlace release];
[theVerb release];
[thePlace release];
[theTemplate release];
[theStory release];

}

-(IBAction)createStory:(id)sender{
int i = 10;
NSLog(@"%i", i);
NSLog(@"creatStory%@",theTemplate.text);
theTemplate.text = [theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text];
theTemplate.text = [theTemplate.text stringByReplacingOccurrencesOfString:@"<verb>" withString: theVerb.text];
theTemplate.text = [theTemplate.text stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text];
theStory.text = theTemplate.text;
NSLog(@"creatStory%@",theStory.text);

}

-(IBAction)hideKeyboard:(id)sender{
NSLog(@"hideKeyboard");
[thePlace resignFirstResponder];
[theVerb resignFirstResponder];
[theNumber resignFirstResponder];
[theTemplate resignFirstResponder];
}
@end


7。运行效果,按下Command+R

       

 

    点击createStory之后的效果为

     


8, 最后一个hideKeyboard方法的实现

    该方法主要就是隐藏键盘,主要的代码如下,就是调用resignFirstResponder   

-(IBAction)hideKeyboard:(id)sender{
NSLog(@"hideKeyboard");
[thePlace resignFirstResponder];
[theVerb resignFirstResponder];
[theNumber resignFirstResponder];
[theTemplate resignFirstResponder];
}
      

好了,这个实例完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐