簡體   English   中英

關於自定義全局UIButton的問題

[英]issue about custom a global UIButton

我已經自定義了一個共享按鈕,並將其放在UINavigationbar中作為一個右按鈕按鈕,我想單擊此按鈕以顯示一個UIActivityViewController ,代碼可以成功構建並運行,但是共享按鈕無法單擊,什么也沒有發生(甚至錯誤),就像禁用共享按鈕一樣。

演示圖片:

有人可以幫我解決嗎? 先感謝您。

碼:

import UIKit

import Font_Awesome_Swift

class shareButton:UIButton{

var button:UIButton = UIButton()

init(button_frame: CGRect, connected: [UIButton]?){
    super.init(frame:CGRect(x: 0, y: 0, width: 20, height: 20))
    self.button.frame = button_frame
    self.button.setFAIcon(icon: FAType.FAShareAlt, iconSize: 22, forState: .normal)
    self.button.setFATitleColor(color: .darkGray)
    self.button.addTarget(self, action:#selector(self.buttonPressed), for: .touchUpInside)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func construct() -> UIButton {
    return self.button
}

@objc func buttonPressed() {
    let url = NSURL.init(string: "http://www.probe-lab.com")
    let items:[Any] = [url!]
    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [.print,
                                                    .assignToContact,.saveToCameraRoll,.addToReadingList,.openInIBooks]
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController?.present(activityViewController, animated: true, completion: nil)
 }
}

在viewcontroller中,我使用了如下按鈕:

override func viewDidLoad() {
    super.viewDidLoad()

    let btn = shareButton(button_frame: CGRect(x:0, y:0, width: 30, height:30), connected:nil ).construct()
    let item = UIBarButtonItem(customView: btn)
    self.navigationItem.rightBarButtonItem = item
}

我得到了一個解決方案並修改了我的代碼,添加了“ var strongSelf:shareButton?” 和“ strongSelf = self”,現在它可以運行並顯示一個activityViewController。 我不知道這是否是最好的解決方案,但至少它能起作用。 我發現它可以在iPhone設備和模擬器上正常運行,但在iPad上崩潰,因此添加“ activityViewController.popoverPresentationController?.sourceView = button”即可解決。

import UIKit
import Font_Awesome_Swift

class ShareButton:UIButton{

  var button:UIButton = UIButton()
  var strongSelf:ShareButton?

  init(button_frame: CGRect, connected: [UIButton]?){
   super.init(frame:CGRect(x: 0, y: 0, width: 20, height: 20))
   self.button.frame = button_frame
   self.button.setFAIcon(icon: FAType.FAShareAlt, iconSize: 22, forState: .normal)
   self.button.setFATitleColor(color: .darkGray)
   self.button.addTarget(self, action:#selector(self.buttonPressed), for: .touchUpInside)
   strongSelf = self
  }

  required init?(coder aDecoder: NSCoder) {
   fatalError("init(coder:) has not been implemented")
  }

  func construct() -> UIButton {
   return self.button
  }

 @objc func buttonPressed() {
  let url = NSURL.init(string: "http://www.probe-lab.com")
  let items:[Any] = [url!]
  let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
  activityViewController.excludedActivityTypes = [.print,
                                                .assignToContact,.saveToCameraRoll,.addToReadingList,.openInIBooks]
  activityViewController.popoverPresentationController?.sourceView = button
  let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController?.present(activityViewController, animated: true, completion: nil)
   }
  }

在任何視圖中,我都想使用此共享按鈕,只需添加以下行:

    override func viewDidLoad() {
    super.viewDidLoad()

    let btn = ShareButton(button_frame: CGRect(x:0, y:0, width: 30, height:30), connected:nil ).construct()
    let item = UIBarButtonItem(customView: btn)
    self.navigationItem.rightBarButtonItem = item
    }

首先,請確保將類名大寫。 這是Swift中的約定。 現在,繼續解決方案:

您無需子類化UIButton即可獲得所需的結果。

只需添加“ buttonPressed”代碼作為barbuttonitem的操作即可。

嘗試創建一個UIButton子類,該子類在內部實例化另一個UIButton並將該按鈕發送到ViewController,然后將其轉換為UIBarButtonItem,這是一種非常復雜的方法。

您可以從此存儲庫下載我的解決方案: https : //github.com/asabzposh/StackOverFlow-48554026.git

如果您希望創建UIButton子類,請按照以下方式進行操作:

import UIKit

class ShareButton: UIButton {

    override init(frame: CGRect) {            
        super.init(frame: frame)
        // Set title, background color, etc.
        let imageView = UIImageView(frame: frame)
        let image = UIImage(named: "p.jpeg")
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        self.addSubview(imageView)
        self.addTarget(self, action: #selector(internalButtonPressed), for: .touchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    @objc func internalButtonPressed() {
        let url = NSURL.init(string: "http://www.probe-lab.com")
        let items:[Any] = [url!]
        let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
        activityViewController.excludedActivityTypes = [.print, .assignToContact,.saveToCameraRoll,.addToReadingList,.openInIBooks]
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController?.present(activityViewController, animated: true, completion: nil)
    }
}

然后在您的ViewController中使用它:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

       // 1. Solution A
       // let item = UIBarButtonItem(title: "Share", style: .plain, target: self, action: #selector(buttonPressed))

      // 2. Solution B
        let button = ShareButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        let item = UIBarButtonItem()
        item.customView = button

     // 3. Finally Add barbutton item
        self.navigationItem.rightBarButtonItem = item
    }

    @objc func buttonPressed() {
        let url = NSURL.init(string: "http://www.probe-lab.com")
        let items:[Any] = [url!]
        let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
        activityViewController.excludedActivityTypes = [.print, .assignToContact,.saveToCameraRoll,.addToReadingList,.openInIBooks]
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController?.present(activityViewController, animated: true, completion: nil)
    }
}

您的自定義類的問題是我剛剛測試了您的自定義類,並在其上運行了小型測試用例,進行了一些更改,請看看。 無需在共享按鈕中創建var button:UIButton = UIButton() ,只需重寫UIbutton方法即可。我也更改了添加目標函數。

class shareButton:UIButton{

override init(frame: CGRect) {
    // set myValue before super.init is called
    super.init(frame: frame)

    //self.button.frame = frame
    isUserInteractionEnabled = true
    backgroundColor = UIColor.red
    addTarget(self, action: #selector(buttonPressed1( sender:)), for: UIControlEvents.touchUpInside)
    // set other operations after super.init, if required
    backgroundColor = .red
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

 @objc func buttonPressed1(sender: UIButton) {

 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM