gpt4 book ai didi

objective-c - 编程 iOS : Document Preview, 更改完成按钮

转载 作者:行者123 更新时间:2023-11-28 19:24:12 25 4
gpt4 key购买 nike

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

最佳答案

将完成按钮更改为完成按钮示例:

我读到您可以从 lastObject 获取导航项并更改左按钮或右按钮。

要更改完成按钮,您需要等待 Controller UIDocumentInteractionController View 完成显示。遗憾的是没有办法知道这一点,但是有:

  • (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *) Controller

这会告诉您它何时开始。

我所做的:添加一个计时器,当 Controller 准备就绪时,然后获取导航栏项目按钮并将其替换为新按钮。

1) 在.h中添加委托(delegate)和定时器

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

关于objective-c - 编程 iOS : Document Preview, 更改完成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com