如何在viewcontroller中隐藏和显示按钮,例如UserRole =" Admin"显示按钮和UserRole ="用户"隐藏按钮。我在下面的AppDelegate类中编写了代码,如何在视图控制器类中隐藏和显示buttonMethod名称的方法是:isItAdminOrNot
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
//var email = (String)()
if (error == nil) {
let givenName = user.profile.name
let email = user.profile.email
let param = ["UserName": givenName!, "Email": email!] as Dictionary<String, String>
let request = NSMutableURLRequest(url: NSURL(string: "http://beta.json-generator.com/api/json/get/EJoC6gB_z")! as URL)
let session = URLSession.shared
request.httpMethod = "POST"
//Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = try! JSONSerialization.data(withJSONObject: param, options: [])
let task = session.dataTask(with: request as URLRequest) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] {
print("Response: \(json)")
let USerresponse = json["UserRole"] as! String
print(USerresponse)
DispatchQueue.main.async {
// now update UI on main thread
DispatchQueue.main.async {
// now update UI on main thread
self.userRoleValue(userRole: USerresponse)
}
}
} else {
let jsonStr = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) // No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
if let data1 = jsonStr?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
do {
if let dictionary = try JSONSerialization.jsonObject(with: data1, options: JSONSerialization.ReadingOptions.mutableContainers) as? [AnyObject] {
let arrayVlaues = dictionary as Array
print(arrayVlaues[0])
let userRole = arrayVlaues[0].value(forKey: "UserRole") as! String
DispatchQueue.main.async {
// now update UI on main thread
self.userRoleValue(userRole: userRole)
}
}
}
}
}
} catch let parseError {
print(parseError)// Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
// ...
} else {
print("\(error.localizedDescription)")
}
}
func userRoleValue(userRole: String) {
if(userRole == "User") {
self.loadParticlularController(value: 1)
} else if(userRole == "Admin") {
self.loadParticlularController(value: 2)
} else if(userRole == "NewUser") {
self.loadParticlularController(value: 0)
}
}
func loadParticlularController(value: Int) {
print(value)
let myStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var pageNav: UINavigationController;
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
// New User
if (value == 0) {
let homePage = myStoryBoard.instantiateViewController(withIdentifier: "ApprovalUser") as! ApprovalUser
pageNav = UINavigationController(rootViewController: homePage)
appDelegate.window?.rootViewController = pageNav
} else if(value == 1) { // Existing User
let homePage = myStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
homePage.isItAdminOrNot(val: false)
pageNav = UINavigationController(rootViewController: homePage)
appDelegate.window?.rootViewController = pageNav
} else if(value == 2) { // Existing User with Admin
let homePage = myStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
homePage.isItAdminOrNot(val: true)
pageNav = UINavigationController(rootViewController: homePage)
appDelegate.window?.rootViewController = pageNav
}
}
答案 0 :(得分:0)
尝试,
如果然后按钮的名称是homePageButton,
homePageButton.isHidden = true
homePageButton.isHidden = false
在HomeViewController的viewDidLoad
或viewWillAppear
方法中显示/隐藏按钮。
答案 1 :(得分:0)
在你的
下homePage.isItAdminOrNot(val: false)
添加:
homePage.button.isHidden = true