ios - 如何从添加到 View 的工具栏中关闭 IOS ViewController

标签 ios textfield viewcontroller

我有一个包含文本字段的 ViewController。当用户开始编辑文本时,我想显示 一个 ViewController,用户可以使用它来选择文本字段的字体。我正在使用 Xcode 5 IOS 7 的 Storyboard和编译。这是我展示 View Controller 的地方...

- (void)textViewDidBeginEditing:(UITextView *)textView
{

    FTViewController *fontSelector = [[FTViewController alloc] init];
    [self presentViewController:fontSelector animated:YES completion:nil];

 }

我正在使用名为 FTFontSelector 的 Cocoapods 项目来选择字体。在获取的 FTViewController 中 呈现,我有以下代码在 View Controller 加载时运行。此代码添加一个 UIToolbar 有两个按钮,其中一个我想用来关闭 View Controller ,但是所有的 我为实现这一目标所做的尝试都没有奏效。

- (void)loadView;
{
  CGRect frame = [[UIScreen mainScreen] applicationFrame];

  self.textView = [[UITextView alloc] initWithFrame:frame];    
  self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  self.textView.textAlignment = NSTextAlignmentLeft;
  self.textView.font = [UIFont systemFontOfSize:FONT_SIZE];
  self.textView.text = @"Hello World!";

  self.changeFontButton = [[UIBarButtonItem alloc] initWithTitle:@"Font"
                                                       style:UIBarButtonItemStyleBordered
                                                      target:self
                                                      action:@selector(changeFont:)];
  self.dismissFontButton = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss"
                                                         style:UIBarButtonItemStyleBordered
                                                        target:self
                                                        action:@selector(dismissFont:)];

  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
  toolbar.tintColor = [UIColor lightGrayColor];
  toolbar.translucent = YES;
  toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  toolbar.items = @[self.changeFontButton, self.dismissFontButton];

  self.textView.inputAccessoryView = toolbar;

  self.view = self.textView;

这是我调用的方法来关闭我需要帮助的 View Controller 。我已经注释掉了 我试过什么。

- (void)dismissFont:(id)sender;
{
    [self.textView resignFirstResponder];
    // This crashes! 
    //[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];

}

我怀疑我的部分问题是我呈现的 View Controller 中没有导航 Controller , 但是当我尝试添加一个时,文本在 View 后面(我可以看到文本顶部的几个像素), 而且没有后退按钮,所以我尝试在那里做的事情是错误的。

我对 IOS 编程还是很陌生,所以还有很多我还不明白。我有剩下的 我的应用程序现在运行良好,但最后一 block 让我头疼。我希望这里有人可以 告诉我如何关闭这个 View Controller 。

最佳答案

[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];

向您的 View Controller 的父 View Controller 发送消息。这显然不是你想要的。充其量它会关闭您的 View Controller 。

[self dismissViewControllerAnimated:YES completion:NULL];

是你应该做的。

来自 -dismissViewControllerAnimated:completion:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

在这种情况下,您的 View Controller (self) 是呈现 View Controller 。

关于ios - 如何从添加到 View 的工具栏中关闭 IOS ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774189/

相关文章:

ios - 如何在 Swift 中使固定区域容纳更大的区域?

ios - CLLocationManager开始监控区域时如何判断用户是否已经在区域内

android - 用于 Android 和 iOS 应用程序的 WebSockets

ios - WKWebView中如何实现 "Are you sure you want to submit the form again"

objective-c - 触发事件的全局监视器后,如何在聚焦的 NSTextfield(在我的应用程序之外)上写入?

java - 用户只能选择 JavaFX 之一的图像按钮

ios - 我如何转至与当前 Controller 共享同一根 Controller 的另一个 viewController?

ios - 使用手势从底部滑动 View Controller

swift - 如何比较文本字段内的文本字段值?

ios - 在点击图像时打开 View