隐藏按钮; role="Admin" => 查看按钮 代码: func-6ren">
gpt4 book ai didi

ios - 如何在AppDelegate中隐藏和显示UIViewController按钮? - swift

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

如何快速隐藏和显示 AppDelegate 文件中的 UIViewController 按钮?例如:role="User" => 隐藏按钮; role="Admin" => 查看按钮

代码:

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:Any]] {
print("Response: \(json)")
for user in json {
if let role = user["UserRole"] as? String{
print(role)

if role == "User"{


DispatchQueue.main.async (execute: { () -> Void in


let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main",bundle:nil)

let page=myStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController


let pageNav = UINavigationController( rootViewController : page )

let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate


appDelegate.window?.rootViewController=pageNav

[self.vc.apps.isHidden]


})





} else if role == "NewUser"
{
DispatchQueue.main.async (execute: { () -> Void in

let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main",bundle :nil)

let page=myStoryBoard.instantiateViewController(withIdentifier: "ApprovalUser") as! ApprovalUser

let pageNav = UINavigationController(rootViewController:page)

let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate


appDelegate.window?.rootViewController=pageNav

})



}else if role == "Admin" {

DispatchQueue.main.async (execute: { () -> Void in



let myStoryBoard:UIStoryboard = UIStoryboard(name:"Main",bundle : nil)

let page=myStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

let pageNav = UINavigationController(rootViewController : page)

let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate

appDelegate.window?.rootViewController=pageNav

})


}
}
}
// if you want role of first object only then use
// print( json . first?["UserRole"])
}
} catch let parseError {
print(parseError)
}
}

最佳答案

您可以使用UserDefaults或全局变量来完成此操作。从父 View Controller 外部访问和修改 UI 元素是一种非常糟糕的做法。

不过,我强烈推荐UserDefaults。在您的 AppDelegate 中,将其添加到您的代码中:

if role == "User"{
UserDefaults.standard.set("User", forKey: "role")
// ... the rest of your code (i.e.: appDelegate.window?.rootViewController=pageNav)
}

else if role == "NewUser"{
UserDefaults.standard.set("NewUser", forKey: "role")
// blah, blah, ... rest of your code goes here
}
else if role == "Admin"{
UserDefaults.standard.set("Admin", forKey: "role")
}

然后在 ViewController 的 viewDidLoad() 中:

 if UserDefaults.standard.value(forKey: "role") != nil{
let role = UserDefaults.standard.value(forKey: "role") as? String ?? "User"
if role == "User" || role == "NewUser" {
yourButton.isHidden = true
// the role of the user does not allow them to see the button
}
else if role == "Admin" { yourButton.isHidden = false }
}
else{
yourButton.isHidden = true
}

在我看来,这是更好的方法,而不是从另一个类访问按钮。如果有帮助请告诉我!

<小时/>

说明:

基本上,您只需将 role 的值保存到 UserDefaults(也称为基于包 ID 的应用程序本地内存),然后在 viewDidLoad 中检索它()。在那里,您将检查角色并根据情况隐藏/显示按钮。

注意:这也处理由于某种原因无法检索信息的情况,将用户视为普通的“用户”。

编辑:

根据OP的请求,我添加了多个用户处理。当新用户登录时,只需从 UserDefaults 中删除这些值,然后调用整个函数即可。它看起来像这样:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {

//var email = (String)()

if UserDefaults.standard.value(forKey: "role") != nil{
UserDefaults.standard.removeObject(forKey: "role")
}

if (error == nil) {
//...
}

}

关于ios - 如何在AppDelegate中隐藏和显示UIViewController按钮? - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157047/

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