繁体   English   中英

UIBarButton 不在代码段之前显示,但在代码段之后显示

[英]UIBarButton doesn't show before section of code but shows after

super.viewDidLoad()

let urlString: String
if navigationController?.tabBarItem.tag == 0 {
    urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100"
} else {
    urlString = "https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100"
}

if let url = URL(string: urlString) {
    if let data = try? Data(contentsOf: url) {
        parse(json: data)
        return
    }
}

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(actionButton))

如果我在代码部分(如上)之后创建rightBarButton ,则条形按钮不会显示。 但是,如果我在代码之前创建rightBarButton ,则会出现rightBarButton 我有一个嵌入在导航控制器和标签栏控制器中的tableViewController 为什么在代码之后制作按钮不显示按钮?

删除return关键字, return不会执行更多的代码行。 请参考与 swift return功能混淆的答案。

你的代码应该是

super.viewDidLoad()
let urlString: String = (navigationController?.tabBarItem.tag == 0) ? "https://api.whitehouse.gov/v1/petitions.json?limit=100" : "https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100"


if let url = URL(string: urlString) {
    if let data = try? Data(contentsOf: url) {
        parse(json: data)
    }
}
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(actionButton)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM