Swift 4中每个ViewController中的注销按钮

时间:2019-03-27 14:32:50

标签: ios swift

我是iOS新手。我已经实现了一个登录部分。登录正常。 但是登录后,我要手动添加一个按钮(注销)。 单击后,从应用程序注销。

我可以制作一个标题,该标题具有家庭和注销链接吗?这样我就不必在所有viewcontroller中重新编码相同的东西。

3 个答案:

答案 0 :(得分:1)

我认为您想使用UINavigationController它将包含您的视图控制器,然后将UIBarButtonItem s添加到navigationController并将在所有viewController上持续存在

答案 1 :(得分:0)

我也在开发一个大型应用程序,这对我有用,我习惯于从任何ViewController管理注销:

       let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "loginVC") as! MMLoginViewController

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

       appDel.window?.rootViewController = loginVC

答案 2 :(得分:0)

我已创建一个 UILibraryFunction.swift 文件。

class UILibraryFunction: UIViewController {

var navBar:UINavigationBar = UINavigationBar()
var navItem = UINavigationItem(title: "SomeTitle")

var screenWidth:CGFloat = 0
var screenHeight:CGFloat = 0

var NameHeight:CGFloat = 0
var NameWidth:CGFloat = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let screenSize: CGRect = UIScreen.main.bounds

    screenWidth = screenSize.width
    screenHeight = screenSize.height


    NameHeight = screenHeight * 0.09
    NameWidth = screenWidth
    navBar = UINavigationBar(frame: CGRect(x: 0, y: 30, width: NameWidth, height: NameHeight))
    self.view.addSubview(navBar)

    navBar.setItems([navItem], animated: false)

}


//---- Right Bar Button -----
func rightBarButton() {
    let rightBarButton = UIBarButtonItem(title: "Logout", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Lge3Generell.Logout(_:)))
    navItem.rightBarButtonItem = rightBarButton

}

//--- Left Bar Button -----
func leftBarButton() {

    let leftBarButton = UIBarButtonItem(title: "Home", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.GoToHome(_:)))
    navItem.leftBarButtonItem = leftBarButton

}

//----- UI Bar Title ----
func setTitle(BarTitle:String) {
    navItem = UINavigationItem(title: BarTitle)
    return navBar.setItems([navItem], animated: false)
}

}

现在,我在所有ViewController的ViewDidLoad中都调用此3函数,如下所示:-

class General: UILibraryFunction {


override func viewDidLoad() {
    super.viewDidLoad()

    //-- top bar ------
    setTitle(BarTitle: "Header")
    rightBarButton()
    leftBarButton()

}
}