简体   繁体   中英

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

If I create the rightBarButton after the section of code (like above) the bar button doesn't show up. However, if I create the rightBarButton before the code, the rightBarButton appears. I have a tableViewController embedded in a navigation controller and tab bar controller. Why does making the button after the code not show the button?

Remove the return keyword, return won't execute further lines of the code. please refer answer of confused with the functionality of return in swift .

Your code should be

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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