objective-c - 想做一个可以讲textfield中的内容合并为一个字符串的功能

浏览:34日期:2023-12-17

问题描述

如题。。。小白学IPHONE 开发,

linke了两个text fild一个button,一个label,想要实现讲两个text field中输入的字符串合并为一个,点一下button就会显示在lable中

附上代码:

@interface HWViewController ()@property (strong, nonatomic) IBOutlet UILabel *combinedWord;@property (strong, nonatomic) IBOutlet UITextField *secondText;@property (strong, nonatomic) IBOutlet UITextField *firstText;@property (strong, nonatomic) IBOutlet UIButton *combineButton;- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.combinedWord.text=@'Wating...';}- (IBAction)myButtonPressed:(id)sender { NSString *combinedWord= textfiled1+textfield2

求高手回答,想自己做一个project来学习,看了document感觉还是不太会,Q

问题解答

回答1:

NSString *combinedWord= [firstText.text stringByAppendingString:secondText.text];

或者

NSString *combinedWord= [NSString stringWithFormat:@'%@%@',firstText.text,secondText.text];

回答2:

- (IBAction)myButtonPressed:(id)sender { self.combinedWord.text = [self.secondText.text stringByAppendingString:self.firstText.text];}

stringByAppendingString用来拼接一个字符串

回答3:

stringWithFormat

相关文章: