UIReferenceLibraryViewController in a popover

2019-06-07 07:38发布

问题:

I have this portion of code to pull up the dictionary if a word is searched:

- (IBAction)searchButtonPressed:(id)sender
{
    NSString *searchTerm = self.searchTextField.text;

    if([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:searchTerm])
    {
        UIReferenceLibraryViewController *referenceLibraryVC = [[UIReferenceLibraryViewController alloc] initWithTerm:searchTerm];
        [self presentModalViewController:referenceLibraryVC animated:NO];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Word not found" message:@"no definition" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

however, it only seems to work on the main view in the xib and covers the entier iPad screen. How can I get it so that it only opens up in a subview, just a portion of the screen?

回答1:

Use a UIPopoverController, like it is used in native applications.

self.popover = [[UIPopoverController alloc] initWithContentViewController:referenceLibraryVC];
[self.popover presentPopoverFromRect:[sender frame] inView:[sender superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

You will need to retain the popover controller until it is dismissed. Set the delegate and you can listen when it is dismissed so you can release it.