我正在使用UIDocumentInteractionController
类来预览文档。是否可以更改完成按钮以用另一个关闭预览?例如,我想设置一个不同的标题:“关闭”而不是“完成”。
问问题
3213 次
2 回答
2
用完成按钮更改完成按钮示例:
我读到您可以从 lastObject 获取导航项并更改向左或向右按钮。
要更改完成按钮,您需要等待控制器 UIDocumentInteractionController 视图完成显示。可悲的是,没有办法知道这一点,但是有:
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
这会告诉你什么时候开始。
我做什么:添加一个计时器,当控制器准备好时,然后获取导航栏项目按钮并将其替换为新的。
1)在 .h 添加这个委托和计时器
.. UIViewController <UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) NSTimer *timer;
2) 在.m
#import "UIView+BK.h"
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
//Start timer
_timer = [NSTimer scheduledTimerWithTimeInterval:.05
target:self
selector:@selector(checkNavigationBar)
userInfo:_timer
repeats:YES]; //YES TO CYCLE
}
- (void) checkNavigationBar
{
//get the last view open (the UIDocumentInteractionController View)
UIView *lastWindow = [[[[UIApplication sharedApplication] keyWindow ] subviews] lastObject];
//Find the controller the view belongs too.
UIViewController *controller = [lastWindow findViewController];
if (controller) {
//find navigation bar using a category
UINavigationBar *bar = [controller.view navigationBarFromView];
//get the navigationItem
UINavigationItem *item = bar.topItem;
//get the done button
UIBarButtonItem *doneButton = item.leftBarButtonItem ;
//Creates the new button
UIBarButtonItem *newDoneButton = [[UIBarButtonItem alloc ]
initWithTitle:@"Finished"
style:UIBarButtonItemStylePlain
target:doneButton.target
action:doneButton.action];
//change done button
item.leftBarButtonItem = newDoneButton;
//Stop timer
[_timer invalidate];
_timer = nil;
}
}
3)你需要这个类别
标题类别:
进口
@interface UIView (BK)
- (UIViewController *)findViewController;
- (UINavigationBar *)navigationBarFromView;
@end
实施类别:
#import "UIView+BK.h"
@implementation UIView (BK)
- (UIViewController *)findViewController {
Class vcc = [UIViewController class]; // Called here to avoid calling it iteratively unnecessarily.
UIResponder *responder = self;
while ((responder = [responder nextResponder])) if ([responder isKindOfClass: vcc]) return (UIViewController *)responder;
return nil;
}
- (UINavigationBar *)navigationBarFromView {
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:[UINavigationBar class]]) {
return (UINavigationBar *)subview;
}
UIView *result = [subview navigationBarFromView];
if (result) {
return (UINavigationBar *)result;
}
}
return nil;
}
@end
于 2013-01-24T02:40:51.393 回答
2
我使用了 QLPreviewController 而不是 UIDocumentInteractionController,并且能够更改“完成”按钮(UIDocumentInteractionController 也调用 QLPreviewController)。在展示 QLPreviewController 之后,我在索引 0 处找到了他的导航控制器作为子视图控制器,然后我替换了左栏按钮项。使用数据源委托将您的 url 提供给 QLPreviewController。
在我的 viewcontroller.h 中:
#import <UIKit/UIKit.h>
#import <QuickLook/QuickLook.h>
@interface MyViewController : UIViewController<QLPreviewControllerDataSource>
{
// Video preview
QLPreviewController *_qlPreviewController;
NSURL *_currentUrl;
}
在我的 viewcontroller.m 中:
#import "MyViewController.h"
@implementation MyViewController
/*
........
*/
-(void)showDocument
{
_currentUrl = [NSURL fileURLWithPath:@"path to your document here"];
_qlPreviewController = [[QLPreviewController alloc] init];
_qlPreviewController.dataSource = self;
[self presentViewController:_qlPreviewController animated:true completion:^(void){
if ([self->_qlPreviewController.childViewControllers count] > 0) {
UINavigationController *nav = (UINavigationController*) self->_qlPreviewController.childViewControllers[0];
UIBarButtonItem *origDoneButton = nav.navigationBar.items[0].leftBarButtonItem;
nav.navigationBar.items[0].leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MyTitle" style:UIBarButtonItemStyleDone target:origDoneButton.target action:origDoneButton.action];
}
}];
}
#pragma mark - QLPreviewControllerDatasource
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return _currentUrl;
}
@end
于 2018-08-29T15:35:09.967 回答