我是目标c的新手,所以开发基本程序。在我的项目中,我有一个View Controller,它有两个文本字段和一个段控件,另外两个视图控制器连接起来,即第一个视图控制器和第二个视图控制器,我的问题是每当我在主视图控制器的文本域中输入数据时(即:段视图控制器),它就不会被传递到其他视图控制器。在这种情况下我应该如何使用performSelectoronMainTread方法。这是我的代码。请帮助我。
Thanks in advance
#import "SecondViewController.h"
#import "FirstViewController.h"
#import "SegmentControlViewController.h"
@interface SegmentControlViewController ()<UITextFieldDelegate>
@end
@implementation SegmentControlViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textField1.delegate = self;
self.textField2.delegate = self;
self.firstView.hidden = false;
self.secondView.hidden = true;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)segmentControlPressed:(id)sender {
switch (self.segmentControlButton.selectedSegmentIndex) {
case 0:
self.firstView.hidden = false;
self.secondView.hidden = true;
break;
case 1:
self.firstView.hidden = true;
self.secondView.hidden = false;
break;
default:
break;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
FirstViewController *firstViewController1 = [[FirstViewController
alloc]init];
SecondViewController *secondViewController1 = [[SecondViewController
alloc]init];
[firstViewController1 displayTextValu:self.textField1.text ];
[secondViewController1 displayTextValu:self.textField2.text];
[textField resignFirstResponder];
return true;
}
}
@end
firstViewcontroller.m
#import "SegmentControlViewController.h"
#import "FirstViewController.h"
@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
}
-(void)displayTextValu:(NSString *) label{
self.firstNameLabel.text = label;
[self.view setNeedsDisplay];
}
@end
类似地编写第二个视图控制器。如何将文本字段数据更新为firstViewController的firstNameLabel
答案 0 :(得分:0)
使用实例变量,而不是创建第一个和第二个视图控制器的新对象。在viewDidLoad中启动它们。另外,在第一个和第二个viewcontrollers中添加NSString变量,比如
NSString *firstLabelText;
并在textƒieldDidEndEditting中,尝试设置变量值。 只需检查此方法
答案 1 :(得分:0)
修改调用以查看这样的控制器:
[firstViewController1 performSelectorOnMainThread:@selector(displayTextValu:) withObject:self.textField1.text waitUntilDone:NO];
但您无需使用performSelectorOnMainThread
,请尝试此GCD通话:
NSString *text1 = self.textField1.text;
NSString *text2 = self.textField2.text;
dispatch_async(dispatch_get_main_queue(), ^{
[firstViewController1 displayTextValu:text1 ];
[secondViewController1 displayTextValu:text2];
});