2010-06-02 25 views
20

Tôi muốn hiển thị 2 tùy chọn như "hi" & "tạm biệt" khi người dùng hoàn thành lựa chọn trên UIWebView.hiển thị menu tùy chỉnh khi chọn trong UIWebView trong iphone

Tôi đã thêm người quan sát vào bộ điều khiển chế độ xem của mình như sau. Nhưng tôi không biết thực hiện thêm.

[[UIMenuController sharedMenuController] addObserver:self 
              forKeyPath:UIMenuControllerWillShowMenuNotification 
              options:nil 
              context:nil 
]; 

Trả lời

44

Sagar,

Câu hỏi của bạn là một vài tháng tuổi, nhưng cuối cùng tôi figured này ra, vì vậy tôi figured tôi muốn trả lời nó trong trường hợp nó giúp ra người khác.

Tôi đã thêm mã sau vào chế độ xemDidAppear: phương pháp của trình điều khiển chế độ xem có chứa chế độ xem web.

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    UIMenuItem *customMenuItem1 = [[[UIMenuItem alloc] initWithTitle:@"Custom 1" action:@selector(customAction1:)] autorelease]; 
    UIMenuItem *customMenuItem2 = [[[UIMenuItem alloc] initWithTitle:@"Custom 2" action:@selector(customAction2:)] autorelease]; 
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]]; 
} 

Trong viewDidDisappear tôi :, tôi đi trước và loại bỏ những mặt hàng:

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 

    [[UIMenuController sharedMenuController] setMenuItems:nil]; 
} 

Sau đó, tôi thực hiện các canPerformAction: withSender: phương pháp trong điều khiển xem. Nó giúp hiểu khái niệm về người trả lời và chuỗi trả lời để hiểu những gì đang xảy ra ở đây. Về cơ bản, uiviewcontroller của bạn là một phần của chuỗi trả lời, vì vậy nó sẽ được hỏi nếu nó có thể xử lý bất kỳ hành động nào (như các hành động tùy chỉnh của bạn mà bạn đã thêm ở trên) mà các đối tượng cao hơn chuỗi trả lời (như UIWebView) không biết cách xử lý (xem số UIResponder documentationEvent Handling Guide for iOS để biết chi tiết đẫm máu).

Bây giờ, khi canPerformAction: withSender: được gọi cho chế độ xem web, thông số người gửi được đặt thành không. Vì vậy, tôi cố gắng một chút thông minh về cách tôi viết chức năng này. Về cơ bản, tôi đảm bảo rằng người gửi là không, tôi đang hiển thị chế độ xem web cho người dùng và bất kỳ điều khiển nào khác trên trang không phải là người trả lời đầu tiên. Nếu đó là trường hợp, sau đó tôi kiểm tra xem đây có phải là một trong những hành động tôi đã xác định ở trên và trả lại CÓ nếu có. Trong tất cả các trường hợp khác, tôi trả về giá trị mặc định từ UIViewController bằng cách gọi phương thức tương tự trên siêu.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
    if (webView.superview != nil && ![urlTextField isFirstResponder]) { 
     if (action == @selector(customAction1:) || action == @selector(customAction2:)) { 
      return YES; 
     } 
    } 

    return [super canPerformAction:action withSender:sender]; 
} 

Tất nhiên, bây giờ bước tiếp theo là tìm hiểu làm thế nào để thực sự làm điều gì đó với sự lựa chọn (có thể bằng cách chạy một số JavaScript trong webview).

+0

Bạn vẫn có thể trao giải cho tôi một tiền thưởng nếu bạn muốn :-) – Jacques

+1

Tôi đã gỡ bỏ việc kiểm tra cho người gửi == nil vì các phiên bản tương lai của iOS có thể đặt người gửi đến một cái gì đó khác hơn là con số không – Jacques

+0

tôi trân trọng kính mời bạn trả lời câu hỏi này http://stackoverflow.com/questions/31183894/uimenucontroller-method-settargetrectinview-not-working-in-uitableview –

3

Trong nhanh chóng:

class ViewController: UIViewController { 
    override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 

     // add two custom menu items to the context menu of UIWebView (assuming in contenteditable mode) 
     let menuItem1 = UIMenuItem(title: "Foo", action: #selector(ViewController.foo)) 
     let menuItem2 = UIMenuItem(title: "Bar", action: #selector(ViewController.bar)) 
     UIMenuController.sharedMenuController().menuItems = [menuItem1, menuItem2] 
    } 

    override func viewDidDisappear(animated: Bool) { 
     super.viewDidAppear(animated) 
     UIMenuController.sharedMenuController().menuItems = nil 
    } 

    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { 
     if webView?.superview != nil { 
      if action == #selector(ViewController.foo) || action == #selector(ViewController.bar) { 
       return true 
      } 
     } 

     return super.canPerformAction(action, withSender: sender) 
    } 

    func foo() { 
     print("foo") 
    } 

    func bar() { 
     print("bar") 
    } 
} 

Lưu ý: #selector có sẵn trong Swift 2.2.

screenshot

Các vấn đề liên quan