故事板 ViewController SecondViewController
在使用小数点在 TextFieldNumber 中输入数字后,我想从模态 SecondViewController 中的 LabelNumber(在 ViewController 中)中传递数据(aNumber,一个变量 NSString)。我在 SecondViewController 中添加了一个关闭按钮(栏按钮项)并连接到 Segue(mySegue)。在 ViewController 中有一个链接到 SecondViewController 的圆形矩形按钮 这是 ViewController.m 中的代码
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *LabelNumber;
@property (weak, nonatomic) SecondViewController * SecondScreen;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString: @"mySegue"]){
self.SecondScreen = segue.destinationViewController;
self.SecondScreen.aNumber = self.LabelNumber.text
}
- (IBAction)close:(UIStoryboardSegue*)sender {
if([sender.identifier isEqualToString:@"mySegue"] ){
self.LabelNumber.text = self.SecondViewController.aNumber;
}
在 SecondViewController.h 我有这个代码:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (strong, nonatomic) NSString* aNumber;
@end
在 SecondViewController.m 我有这个代码:
#import "ViewController.h" #import "SecondViewController.h" @interface SecondViewController () <UITextFieldDelegate>
@property(强,非原子)IBOutlet UITextField *TextFieldNumber; @结尾
- (void)viewDidLoad
{
[super viewDidLoad];
[self.TextFieldNumber becomeFirstResponder];
self.TextFieldNumber.delegate = self;
self.TextFieldNumber.text = self.aNumber;
}
- (IBAction)getValue:(UITextField *)sender {
self.aNumber = self.TextFieldNumber.text ;
[self.TextFieldNumber resignFirstResponder];
}
最后,我总是在 SecondViewController.m 中添加了这一点,即使我不想关闭键盘但我希望它始终保持打开状态并希望在关闭按钮中捕获 TextNumber 值(aNumber)(模态展开)。
-(BOOL) textFieldShouldReturn:(UITextField *)textField{ if (textField == self.TextFieldNumber) [self.TextFieldNumber resignFirstResponder]; 返回是;}