如何在QLPreviewController中添加一个按钮

时间:2011-06-27 08:45:12

标签: iphone ios ipad

是否可以在工具栏或QLpreviewController右上方的actionButton中添加新按钮?

如果是,我怎么写代码?

2 个答案:

答案 0 :(得分:1)

虽然QLPreviewControllerUIViewController的子类,但更改导航项无效。例如,这段代码理论上应该可以工作但不会:

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.navigationItem.rightBarButtonItem = 
   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                             target:self action:@selector(share)] 

但是,您可以向QLPreviewController添加工具栏。首先设置工具栏以显示加载视图的时间:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.toolbarHidden = YES;
}

接下来,从QLPreviewController委托:

设置toolbarItems
- (id)previewController:(QLPreviewController *)previewController 
                 previewItemAtIndex:(NSInteger)idx {

UIBarButtonItem *testButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test"
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:@selector(testButtonTapped:)];

NSArray *myToolbarItems = [NSArray arrayWithObjects:testButtonItem, nil];
previewController.toolbarItems = myToolbarItems;
[testButtonItem release];
}

答案 1 :(得分:1)

尝试在自定义 rightBarButtonItem 类的 viewDidLoad 中设置 QLPreviewController,如下所示:

import QuickLook
import UIKit

class FileViewController: QLPreviewController {
     override func viewDidLoad() {
          super.viewDidLoad()
          navigationItem.rightBarButtonItem = yourButton
     }
}