Ios textFieldShouldBeginEditing在展开序列后调用

Ios textFieldShouldBeginEditing在展开序列后调用,ios,objective-c,uitextfield,unwind-segue,Ios,Objective C,Uitextfield,Unwind Segue,我有两个视图控制器 如果单击第一个视图控制器中的文本字段,则用户将转到第二个视图控制器 第二个视图控制器具有表视图,当进行选择时,它使用“展开”序列将用户带回第一个视图控制器 第一视图控制器 - (IBAction)unwindFromModalViewController:(UIStoryboardSegue *)segue { if ([segue.sourceViewController isKindOfClass:[SecondViewController class]])

我有两个视图控制器

如果单击第一个视图控制器中的文本字段,则用户将转到第二个视图控制器

第二个视图控制器具有表视图,当进行选择时,它使用“展开”序列将用户带回第一个视图控制器

第一视图控制器

- (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;
        }
    }
}
第二视图控制器

- (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,并将用户带回第二个视图控制器

是什么原因导致此问题,我如何解决此问题


谢谢。我举了个例子。注意,当我们在委托方法中将文本设置为文本视图时,
textFieldShouldBeginEditing
没有被调用,这是我们想要的。在故事板中,必须设置推送序列id“toSecondVC”。顺便说一句,我不明白你为什么在这里使用
UITextField
,因为你不能手动编辑文本

FirstViewController.h

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<UITextFieldDelegate, SecondViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UITextField *textField;

@end
#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.h

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<UITextFieldDelegate, SecondViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UITextField *textField;

@end
#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

我找到了答案。问题在于textFieldShouldBeginEditing返回YES,这将保持循环

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == self.textField)
    {
        SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    }
    return NO;
}

删除
后返回YES它解决了问题。

我尝试辞去FirstResponder,但仍然无法工作。我也尝试过亲昵,但没有成功:(我正在尝试将用户导航到具有表视图的第二个视图,当进行选择时,用户返回到第一个视图,文本字段的文本设置为selectionJust found添加
self.textfield.enabled=NO
停止问题,但无法单击文本字段调用第二个视图控制器我已经这样做了,但没有luckin文本。)字段是否应该开始编辑您是否尝试过辞职FirstResponder?我想知道,既然我们无法编辑文本,为什么您在这里使用文本字段而不是标签或按钮?只是为了外观。是否有理由不使用文本字段?如果您不打算编辑文本,那么您可以使用另一个不可编辑控件,并避免使用
textfield应该开始编辑
Hmm…喜欢哪一个?例如,我不喜欢标签
ui按钮