2011-05-02 22 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