Question

I have a ViewController that contains a textfield. When the user begins editing the text, I want to display a ViewController that the user can use to select the font for the textfield. I am using Xcode 5 with storyboards and compiling for IOS 7. This is where I present the view controller...

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

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

 }

I am using a Cocoapods project called FTFontSelector to pick the font. In the FTViewController that gets presented, I have the following code that runs when the view controller loads. This code adds a UIToolbar with two buttons, one of which I would like to be used to dismiss the view controller, but all of the attempts I have made at making this happen have not worked.

- (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;

}

This is the method I call to dismiss the view controller that I need help with. I have commented out what I have tried.

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

}

I suspect part of my problem is that there is no navigation controller in my presented view controller, but when I tried to add one, the text went behind the view (I could see a few pixels of the top of the text), and there was no back button, so something I tried to do there was wrong.

I am still pretty new to IOS programmng, so there is a lot I still don't understand. I have the rest of my app working pretty well now, but this last piece is giving me a headache. I am hoping someone here can show me how to dismiss this view controller.

Was it helpful?

Solution

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

sends a message to the parent view controller of your view controller. This is clearly not what you want. At best it would dismiss your view controller.

[self dismissViewControllerAnimated:YES completion:NULL];

is what you should do.

From -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.

In this case your view controller (self) is the presenting view controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top