我有2个视图控制器。
如果单击第一个视图控制器的文本字段,则会将用户带到第二个视图控制器。
第二个视图控制器具有表视图,当进行选择时,它会使用户使用展开segue返回第一个视图控制器。
第一视图控制器
- (IBAction)unwindFromModalViewController:(UIStoryboardSegue *)segue
{
if ([segue.sourceViewController isKindOfClass:[SecondViewController class]])
{
SecondViewController *secondVC = segue.sourceViewController;
if (secondVC.selectedData)
{
self.textField.text = secondVC.selectedData;
}
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == self.textField)
{
SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
return YES;
}
return NO;
}
第二视图控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *selectedRow = [self.json objectAtIndex:indexPath.row];
self.selectedData = [selectedRow objectForKey:@"data"];
[self performSegueWithIdentifier:@"tofirstVC" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[FirstViewController class]])
{
FirstViewController *firstVC = segue.destinationViewController;
if (self.selectedData)
{
firstVC.textField.text = self.selectedData;
}
}
}
用户返回第一个视图控制器后,将调用textFieldShouldBeginEditing,并将用户带回第二个视图控制器。
造成这种情况的原因是什么,以及如何解决这个问题?
谢谢。
答案 0 :(得分:0)
我做了一个例子。请注意,当我们在委托方法中将文本设置为文本视图时,textFieldShouldBeginEditing
未被调用,正如我们所希望的那样。在storyboard中,必须设置segue id“toSecondVC”。顺便说一下,我不明白为什么你在这里使用UITextField
,因为无论如何都无法手动编辑文本。
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController<UITextFieldDelegate, SecondViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setText:(NSString *)text
{
_textField.text = text;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == self.textField)
{
[self performSegueWithIdentifier:@"toSecondVC" sender:self];
return YES;
}
return NO;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"toSecondVC"]) {
SecondViewController *vc = segue.destinationViewController;
vc.delegate = self;
}
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
- (void)setText:(NSString*)text;
@end
@interface SecondViewController : UITableViewController
@property (nonatomic, strong) NSString *selectedData;
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@interface SecondViewController ()
{
NSArray *_data;
}
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
_data = @[@"first", @"second", @"third"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = _data[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedData = _data[indexPath.row];
[_delegate setText:_selectedData];
if ([_delegate isMemberOfClass:[FirstViewController class]]) {
[(FirstViewController*)_delegate dismissViewControllerAnimated:YES completion:nil];
}
}
@end
答案 1 :(得分:0)
我找到了答案。问题是textFieldShouldBeginEditing返回YES,它保持循环。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == self.textField)
{
SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
}
return NO;
}
删除return YES;
后,它解决了问题。