Objective c 编程iOS:文档预览,更改“完成”按钮

Objective c 编程iOS:文档预览,更改“完成”按钮,objective-c,cocoa-touch,ios,uikit,uidocumentinteraction,Objective C,Cocoa Touch,Ios,Uikit,Uidocumentinteraction,我正在使用UIDocumentInteractionController类预览文档。是否可以更改“完成”按钮,以便用另一个按钮关闭预览?例如,我想设置一个不同的标题:“关闭”而不是“完成”。使用完成按钮更改完成按钮示例: 我了解到,您可以从lastObject获取导航项并更改左侧或右侧按钮 要更改“完成”按钮,需要等待控制器UIDocumentInteractionController视图完成显示。遗憾的是,没有办法知道这一点,但有: (无效)DocumentInteractionContro

我正在使用
UIDocumentInteractionController
类预览文档。是否可以更改“完成”按钮,以便用另一个按钮关闭预览?例如,我想设置一个不同的标题:“关闭”而不是“完成”。

使用完成按钮更改完成按钮示例:

我了解到,您可以从lastObject获取导航项并更改左侧或右侧按钮

要更改“完成”按钮,需要等待控制器UIDocumentInteractionController视图完成显示。遗憾的是,没有办法知道这一点,但有:

  • (无效)DocumentInteractionController将开始审查:(UIDocumentInteractionController*)控制器
这会告诉你什么时候开始

我要做的是:添加一个计时器,当控制器准备好后,获取navigationbar项按钮,并用一个新的替换它

1) 在.h中添加此委托和计时器

.. UIViewController <UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) NSTimer *timer;
3) 你需要这个类别

标题类别:

#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
进口 实施类别:

#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

使用完成按钮更改完成按钮示例:

我了解到,您可以从lastObject获取导航项并更改左侧或右侧按钮

要更改“完成”按钮,需要等待控制器UIDocumentInteractionController视图完成显示。遗憾的是,没有办法知道这一点,但有:

  • (无效)DocumentInteractionController将开始审查:(UIDocumentInteractionController*)控制器
这会告诉你什么时候开始

我要做的是:添加一个计时器,当控制器准备好后,获取navigationbar项按钮,并用一个新的替换它

1) 在.h中添加此委托和计时器

.. UIViewController <UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) NSTimer *timer;
3) 你需要这个类别

标题类别:

#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
进口 实施类别:

#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

我使用了QLEVIEWCONTROLLER而不是UIDocumentInteractionController,并且能够更改“完成”按钮(UIDocumentInteractionController也称为QLEVIEWCONTROLLER)。在展示QLViewController之后,我发现他的导航控制器作为子视图控制器位于索引0处,然后我替换了左栏按钮项。使用datasource委托向QLVeviewController提供url

在my viewcontroller.h中:

#import <UIKit/UIKit.h>
#import <QuickLook/QuickLook.h>

@interface MyViewController : UIViewController<QLPreviewControllerDataSource>
{    
    // Video preview
    QLPreviewController *_qlPreviewController;
    NSURL *_currentUrl;
}
#导入
#进口
@接口MyViewController:UIViewController
{    
//视频预览
QLPreviewController*\u QLPreviewController;
NSURL*_currentUrl;
}
在my 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
#导入“MyViewController.h”
@MyViewController的实现
/*
........
*/
-(作废)展示文件
{
_currentUrl=[NSURL fileURLWithPath:@“此处的文档路径”];
_qlPreviewController=[[qlPreviewController alloc]init];
_qlPreviewController.dataSource=self;
[自我呈现视图控制器:qlPreviewController动画:真实完成:^(无效){
如果([self->qlPreviewController.childViewController计数]>0){
UINavigationController*nav=(UINavigationController*)self->qlPreviewController.childViewControllers[0];
UIBarButtonim*origDoneButton=nav.navigationBar.items[0]。LeftBarButtonim;
nav.navigationBar.items[0]。LeftBarButtonim=[[UIBarButtonim alloc]initWithTitle:@“MyTitle”样式:UIBarButtonimStyleDone目标:origDoneButton.target操作:origDoneButton.action];
}
}];
}
#pragma标记-qlPreviewController数据源
-(NSInteger)PreviewWitemsInPreviewController的编号:(QLPreviewController*)控制器{
返回1;
}
-(id)previewController:(QLPreviewController*)控制器PreviewWitematindex:(NSInteger)索引{
返回_currentUrl;
}
@结束

我使用了QLViewController而不是UIDocumentInteractionController,并且能够更改“完成”按钮(QLViewController也被UIDocumentInteractionController调用)。在展示QLViewController之后,我发现他的导航控制器作为子视图控制器位于索引0处,然后我替换了左栏按钮项。使用datasource委托向QLVeviewController提供url

在my viewcontroller.h中:

#import <UIKit/UIKit.h>
#import <QuickLook/QuickLook.h>

@interface MyViewController : UIViewController<QLPreviewControllerDataSource>
{    
    // Video preview
    QLPreviewController *_qlPreviewController;
    NSURL *_currentUrl;
}
#导入
#进口
@接口MyViewController:UIViewController
{    
//视频预览
QLPreviewController*\u QLPreviewController;
NSURL*_currentUrl;
}
在my 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
#导入“MyViewController.h”
@MyViewController的实现
/*
........
*/
-(作废)展示文件
{
_currentUrl=[NSURL fileURLWithPath:@“此处的文档路径”];
_qlPreviewController=[[qlPreviewController alloc]init];
_qlPreviewController.dataSource=self;
[自我呈现视图控制器:qlPreviewController动画:真实完成:^(无效){
如果([self->qlPreviewController.childViewController计数]>0){
UINavigationController*nav=(UINavigationController*)self->qlPreviewController.childViewControllers[0];
UIBarButtonim*origDoneButton=nav.navigationBar.items[0]。LeftBarButtonim;
nav.navigationBar.items[0]。LeftBarButtonim=[[UIBarButtonim alloc]initWithTitle:@“MyTitle”样式:UIBarButtonimStyleDone目标:origDoneButton.target操作:origDoneButton.action];
}
}];
}
#pragma标记-qlPreviewController数据源
-(NSInteger)PreviewWitemsInPreviewController的编号:(QLPreviewController*)控制器{
返回1;
}
-(id)previewController:(QLPreviewController*)控制器PreviewWitematindex:(NSInteger)索引{
返回_currentUrl;
}
@结束

我的上帝。。。你这么做是为了改变措辞?我希望这是一个高薪客户为了理智而提出的要求,lolHi,我在iOS下尝试过这种解决方法,但它不起作用。。。你更新了这个代码还是找到了另一个解决方案?天哪。。。你这么做是为了改变措辞?我希望这是一个高薪客户为了理智而提出的要求,lolHi,我尝试过这种解决方法