gpt4 book ai didi

ios - 关于自定义全局UIButton的问题

转载 作者:行者123 更新时间:2023-11-30 11:48:37 26 4
gpt4 key购买 nike

我自定义了一个分享按钮,放在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)
}
}

在 View Controller 中我使用了这样的按钮:

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)
}
}

关于ios - 关于自定义全局UIButton的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48554026/

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