1. 字符串连接:
创建一个Single View Application工程。
1)修改XXXViewController.h文件:
#import <UIKit/UIKit.h>
@interface CVUViewController : UIViewController
//声明两个获取字符串的和一个合并用的文本框
@property (strong, nonatomic) IBOutlet UITextField * tf1;
@property (strong, nonatomic) IBOutlet UITextField * tf2;
@property (strong, nonatomic) IBOutlet UITextField * tfLink;
//声明执行合并的方法
- (IBAction)doLink;
@end
2)修改XXXViewController.m文件:
#import "CVUViewController.h"
@interface CVUViewController ()
@end
@implementation CVUViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//视图加载后初始化
self.tf1.text=@"文本1,";
self.tf2.text=@"文本2。";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//实现.h中定义的,关联到按钮的 doLink 方法
- (IBAction)doLink
{
//保存两段文本
NSString *str1 = self.tf1.text;
NSString *str2 = self.tf2.text;
/* 三种方法依次测试 */
//方法1.
//NSString * strLink = [[NSString alloc] initWithFormat:@"%@,%@", str1, str2 ];
//方法2.
//NSString * strLink = [str1 stringByAppendingString:str2];
//方法3 ,本例无效.
NSString * strLink = [strLink stringByAppendingFormat:@"%@,%@",str1, str2];
//更新合并文本框
self.tfLink.text = strLink;
}
@end
3)修改XXXViewController.xib文件:
4)测试:
2. 再连接:
新建工程。
1)创建一个新的类:
2)编写.h文件:
3)编写.m文件:
4)修改XXXViewController.h文件:
5)修改XXXViewController.m文件:
#import "CVUViewController.h"
//导入自定义类
#import "StrCtrl.h"
@interface CVUViewController ()
@end
@implementation CVUViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//视图加载后执行对文本框的初始化
self.tf1.text=@"文本1,";
self.tf2.text=@"文本2。";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//初始化方法
-(IBAction)doLink
{
//创建对象
StrCtrl * strCtrl = [[StrCtrl alloc] init];
//初始化对象字符串
strCtrl.str1 = self.tf1.text;
strCtrl.str2 = self.tf2.text;
//调用方法
[strCtrl doLink];
//更新目标文本框
self.tfLink.text = strCtrl.strLink;
}
@end
6)测试:
- end