1. 字符串连接:
创建一个Single View Application工程。
1)修改XXXViewController.h文件:
1#import <UIKit/UIKit.h> 2 3@interface CVUViewController : UIViewController 4 5 6 //声明两个获取字符串的和一个合并用的文本框 7 @property (strong, nonatomic) IBOutlet UITextField * tf1; 8 @property (strong, nonatomic) IBOutlet UITextField * tf2; 9 @property (strong, nonatomic) IBOutlet UITextField * tfLink; 10 11 //声明执行合并的方法 12 - (IBAction)doLink; 13 14 15@end
2)修改XXXViewController.m文件:
1#import "CVUViewController.h" 2 3@interface CVUViewController () 4 5@end 6 7@implementation CVUViewController 8 9- (void)viewDidLoad 10{ 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view, typically from a nib. 13 14 //视图加载后初始化 15 self.tf1.text=@"文本1,"; 16 self.tf2.text=@"文本2。"; 17 18} 19 20- (void)didReceiveMemoryWarning 21{ 22 [super didReceiveMemoryWarning]; 23 // Dispose of any resources that can be recreated. 24} 25 26//实现.h中定义的,关联到按钮的 doLink 方法 27- (IBAction)doLink 28{ 29 //保存两段文本 30 NSString *str1 = self.tf1.text; 31 NSString *str2 = self.tf2.text; 32 33 /* 三种方法依次测试 */ 34 35 //方法1. 36 //NSString * strLink = [[NSString alloc] initWithFormat:@"%@,%@", str1, str2 ]; 37 38 //方法2. 39 //NSString * strLink = [str1 stringByAppendingString:str2]; 40 41 //方法3 ,本例无效. 42 NSString * strLink = [strLink stringByAppendingFormat:@"%@,%@",str1, str2]; 43 44 //更新合并文本框 45 self.tfLink.text = strLink; 46} 47 48 49@end
3)修改XXXViewController.xib文件:

4)测试:

2. 再连接:
新建工程。
1)创建一个新的类:
2)编写.h文件:
3)编写.m文件:

4)修改XXXViewController.h文件:
5)修改XXXViewController.m文件:
1#import "CVUViewController.h" 2 3 4//导入自定义类 5#import "StrCtrl.h" 6 7 8@interface CVUViewController () 9 10@end 11 12@implementation CVUViewController 13 14- (void)viewDidLoad 15{ 16 [super viewDidLoad]; 17 // Do any additional setup after loading the view, typically from a nib. 18 19 20 //视图加载后执行对文本框的初始化 21 self.tf1.text=@"文本1,"; 22 self.tf2.text=@"文本2。"; 23 24} 25 26- (void)didReceiveMemoryWarning 27{ 28 [super didReceiveMemoryWarning]; 29 // Dispose of any resources that can be recreated. 30} 31 32//初始化方法 33-(IBAction)doLink 34{ 35 //创建对象 36 StrCtrl * strCtrl = [[StrCtrl alloc] init]; 37 38 //初始化对象字符串 39 strCtrl.str1 = self.tf1.text; 40 strCtrl.str2 = self.tf2.text; 41 42 //调用方法 43 [strCtrl doLink]; 44 45 //更新目标文本框 46 self.tfLink.text = strCtrl.strLink; 47} 48 49@end
6)测试:

- end