【问题标题】:UITextField keyboard dismiss on button click to UIApplication methodUITextField 键盘关闭按钮单击到 UIApplication 方法
【发布时间】:2023-04-10 20:43:01
【问题描述】:

我将从情节提要到 xib 中的 UISplitViewController。我想注销我的故事板视图,这会将我带到 UISplitViewController。在情节提要控制器中,我有一个 UITextField。我将其设置为在按 Enter 后它不会自行消失。但是,即使我切换到 splitviewcontroller,它现在仍然保持打开状态,这显然是我不想要的,因为我想注销。

退出按钮

  UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithTitle:@"Log Out" style:UIBarButtonItemStylePlain target:self action:@selector(confirmLogout)];

调出UISplitViewCOntroller的方法

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{

    if (alertView.tag==TAG_DEV){


        if (buttonIndex) {


            [[[UIApplication sharedApplication] delegate] performSelector:@selector(gotoSplitViewController)];


        } else {



            [self.navigationController popViewControllerAnimated:YES];

        }}

  }

委托中的 gotoSplitViewController 方法:

-(void)gotoSplitViewController{




    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *listNC = [[UINavigationController alloc] initWithRootViewController:listVC];

    AssignedViewController *assignedVC = [[AssignedViewController alloc] initWithNibName:@"AssignedViewController" bundle:nil];
    UINavigationController *assignedNC = [[UINavigationController alloc] initWithRootViewController:assignedVC];

    //assign the views to the new nav view controllers
    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.delegate = assignedVC;
    splitViewController.viewControllers = @[listNC, assignedNC];

    listVC.detailViewController = assignedVC;


    [[self window] setRootViewController:splitViewController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];



}

我的 UITextField

self.textBox = [[UITextField alloc] initWithFrame: CGRectMake(10, 660, 750, 90)];
    self.textBox.backgroundColor = [UIColor colorWithRed:(209/255.0) green:(236/255.0) blue:(232/255.0) alpha:1];
    self.textBox.returnKeyType = UIReturnKeySend;
    self.textBox.delegate = self;
    [self.view addSubview:self.textBox];

我在各个地方都尝试过 resignFirstResponder,包括添加

- (void)viewWillDisappear:(BOOL)animated
{
    [self.textBox resignFirstResponder];

}

但是,它们都不起作用。视图更改后如何让键盘消失?

【问题讨论】:

  • 你可以尝试的一件事,如果你还没有这样做的话,你可以在confirmLogout中resignFirstResponder。我能想到的另一件事是,当您在 viewWillDisappear 中调用它时,您的 textBox 可能不再是第一响应者。因此,您可以强制它成为FirstResponder,因为键盘已经可见,并且在下一行中只需 resignFirstResponder。
  • 嗨@nstein!谢谢您的帮助。我确实在 confirmLogout 中尝试过,但还是不行
  • 嗯,我仍然无法让它工作。你认为它与 self.chatBox.delegate = self; 有什么关系吗?行吗?
  • 好的,我发现了一个关于 self.chatbox.delegate.self 的行为。当我删除那条线时,当我改变视图时它会消失,所以这很好。但是,现在与文本字段关联的 uitextview 将不会收到消息,因此无法正常工作。但我想我必须取消授权......你对这将如何工作有任何想法吗?
  • 刚刚回答了我自己提出的问题,我将其设置为 self.chatbox.delegate=nil;

标签: objective-c uitextfield uisplitviewcontroller uiapplicationdelegate resignfirstresponder


【解决方案1】:

我设置

self.chatBox.delegate=nil;

在我的方法中切换视图

【讨论】:

    猜你喜欢
    • 2014-02-12
    • 2011-09-15
    • 2017-08-28
    • 2012-10-12
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多