我是iOS新手。我正在构建一个示例项目。我的项目中有四个viewControllers。在第一个中有两个文本字段。一个是进入国家,另一个是进入城市。点击它们中的每一个,完成第二个viewController。在第二个,第三个和第四个viewControllers中,我在每个中都有tableViews。
第二个viewController中的表视图我有一个国家列表,第三个viewController中的表视图我有一个状态列表,在第四个viewController的表视图中我有一个城市列表。
点击每个tableview的单元格推送到下一个。
现在我的要求是,当我点击国家/地区文本字段时,推送将仅执行到第二个viewController,当我点击城市时,textField推送将完成到第四个viewController。
我想知道的是,如何知道推送是从哪个textField完成的。
感谢任何帮助。提前谢谢。
这是我的第一个viewController实现文件......
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.txtCity.delegate = self;
self.txtCountry.delegate = self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 1) {
[self performSegueWithIdentifier:@"countryScene" sender:self];
} else{
[self performSegueWithIdentifier:@"countryScene" sender:self];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我的第二个viewController实现文件..
#import "CountryViewController.h"
@interface CountryViewController ()
@end
@implementation CountryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrCountries = @[@"India", @"Bangladesh", @"Australia", @"New Zealand", @"South Africa", @"West Indies", @"Sri Lanka", @"England", @"Argentina", @"Brazil"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"countryCell"];
UILabel *lblCountry = (UILabel*)[cell.contentView viewWithTag:3];
lblCountry.text = [self.arrCountries objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrCountries count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier: @"stateScene" sender: self];
}
@end
这是我的第三个viewController实现文件:
#import "StateViewController.h"
@interface StateViewController ()
@end
@implementation StateViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrStates = @[@"West Bengal", @"Uttar Pradesh", @"Madhya Pradesh", @"Jharkhand", @"Bihar", @"Tamilnadu", @"Myanmar", @"Arunachal Pradesh", @"Assam", @"Goa"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell"];
UILabel *lblStates = (UILabel*)[cell.contentView viewWithTag:4];
lblStates.text = [self.arrStates objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrStates count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier: @"cityScene" sender: self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我的第四个viewController实现文件......
#import "CityViewController.h"
@interface CityViewController ()
@end
@implementation CityViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrCities = @[@"Kolkata", @"Bangalore", @"Chennai", @"Mumbai", @"Hyderabad", @"Mangalore", @"New York", @"London", @"Rio de Janeiro", @"Buenos Aires"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
UILabel *lblCities = (UILabel*)[cell.contentView viewWithTag:5];
lblCities.text = [self.arrCities objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrCities count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier: @"showRecipeDetail" sender: self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
我认为你已经为textfield提供了标签。如果标签是1,那么国家文本字段,否则是城市文本字段。通过识别标签,你可以切换到相应的视图控制器。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 1) {
//Country view Controller
[self performSegueWithIdentifier:@"countryScene" sender:self];
}
else{
//City View Controller
[self performSegueWithIdentifier:@"cityScene" sender:self];
}
return YES;
}
答案 1 :(得分:0)
好的,
创建一个实例变量,用于保存所选文本字段的标记。
@interface ViewController() {
NSInteger tagSelectedTextField;
}
在您的第一个ViewController
中,使用setTag:
方法为您的TextField分配标记。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.textCity setTag:1];
[self.textCountry setTag:2];
self.txtCity.delegate = self;
self.txtCountry.delegate = self;
}
现在当你在代表中时,检查标签并相应地执行segue。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 1) {
[self performSegueWithIdentifier:@"cityScene" sender:self];
} else{
[self performSegueWithIdentifier:@"countryScene" sender:self];
}
tagSelectedTextField = textField.tag;
return YES;
}
现在,当您返回此屏幕或使用通知系统时,请使用此标记通过[self.view viewWithTag:tagSelectedTextField]
取回所选文本字段并相应地更新文本。