2011-05-02 47 views
0

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

+0

有下一个不同的问题有很大几个答案,如果有人在这里结束了:http://stackoverflow.com/questions/6855008/change-color-of-uidocumentinteractioncontroller-nav-酒吧 – TahoeWolverine 2015-03-24 19:24:40

回答

2

改变完成按钮以完成按钮实例:

我读了你可以从lastObject导航项目并改变向左或向右按​​钮。

要更改完成按钮,您需要等待控制器UIDocumentInteractionController视图才能完成显示。可悲的是有没有知道的方法,但有:

  • (无效)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)控制器

这会告诉你,它开始时。

我该怎么做:添加一个计时器,当控制器准备就绪,然后获取导航栏项目按钮并将其替换为新的。在.H

1)在.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添加这个代理和定时器

.. UIViewController <UIDocumentInteractionControllerDelegate> 
@property (nonatomic, strong) NSTimer *timer; 

2)),则需要此类别

头类别:

进口

@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 
+2

我的上帝......你做了所有这些改变措辞?我希望这是一个高收入客户的需求,为了健全起见大声笑 – mafiOSo 2014-01-20 04:04:57

+0

嗨,我已经尝试了这种解决方法,在iOS下,它不工作......你更新了这个代码或找到了另一个解决方案? – Dodgson86 2015-07-14 10:51:57