首页 > 解决方案 > 为什么 ui 元素以 swift 编程方式在视图控制器之间共享?

问题描述

我试图以编程方式快速导航视图控制器。所以,我这次测试的目的是

所以这是我的AppDelegate.swift。我创建了导航控制器并嵌入了ViewController.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)

        let navigationController = UINavigationController();
        let mainViewController = ViewController();
        let loginViewController = LoginViewController();
        navigationController.viewControllers = [ mainViewController, loginViewController ];
        self.window?.rootViewController = navigationController;
        self.window?.makeKeyAndVisible()

        return true
    }

然后在视图控制器中

let button: UIButton = {
        let button = UIButton(frame: CGRect(x: 120, y: 120, width: 50, height: 50));
        button.setTitle("Login", for: .normal);
        button.layer.borderColor = UIColor.lightGray.cgColor;
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.backgroundColor = UIColor.red;
        button.layer.cornerRadius = 5;

        return button;
    }();

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white;
        view.addSubview(button);


        button.addTarget(self, action: #selector(loginPressed(_sender:)), for: .touchUpInside);
        // Do any additional setup after loading the view.
    }

    @IBAction func loginPressed(_sender: UIButton) {
        print("clicked");
        let loginViewController = LoginViewController();
        self.navigationController?.pushViewController(loginViewController, animated: true);
    }

而在LoginViewController

let emailLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 150, y: 150, width: 150, height: 150));
        label.text = "Email address";
        return label;
    }();

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(emailLabel);
        print("this is loginViewController");
    }

这是我的结果。在视图控制器中

在此处输入图像描述

在 LoginViewController 中。 在此处输入图像描述

所以这是我的问题:为什么按钮会出现在 loginViewController 中?为什么 uielement 已在视图控制器之间共享?

注意:我尝试在谷歌中搜索。也许因为我是一名网络开发人员,我可能没有用正确的词进行搜索。

标签: iosswiftuiview

解决方案


运行您发布的确切LoginViewController代码,初始视图将是您的(不是您的“主”视图控制器),屏幕将是黑色的。

所以,要修复你那里的东西......

AppDelegate不要尝试创建LoginViewController- 仅创建和设置您的“主要”VC:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)

    let navigationController = UINavigationController();
    let mainViewController = ViewController();

    // don't try to create login view here...
    //let loginViewController = LoginViewController();
    //navigationController.viewControllers = [ mainViewController, loginViewController ];

    // set only your first controller
    navigationController.viewControllers = [ mainViewController ];

    self.window?.rootViewController = navigationController;
    self.window?.makeKeyAndVisible()

    return true
}

现在,运行应用程序时的初始视图将显示带有Login按钮的“主”VC……但是,点击该按钮将进入黑屏,因为您没有提供LoginViewController背景颜色:

class LoginViewController: UIViewController {

    let emailLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 150, y: 150, width: 150, height: 150));
        label.text = "Email address";
        return label;
    }();

    override func viewDidLoad() {
        super.viewDidLoad()

        // set background to green, to make it really obvious
        view.backgroundColor = .green

        view.addSubview(emailLabel);
        print("this is loginViewController");
    }
}

如果您仍然看到Login带有电子邮件标签的按钮,并且/或者背景不是绿色,我怀疑您是从在 Storyboard 中布置视图控制器开始的,但没有清除Main Interface项目设置(常规选项卡):

在此处输入图像描述

如果该Main Interface框不为空,请删除其中的内容并再次运行您的应用程序(进行上述更改)。


编辑

这是一个完整的示例,将您的代码与我描述的编辑一起使用。

  • 创建一个新项目
  • 删除 Main.storyboard
  • AppDelegate将下面的代码复制并粘贴到AppDelegate.swift
  • ViewController将下面的代码复制并粘贴到ViewController.swift
  • MainMain Interface项目设置/常规中删除单词
  • 运行应用程序

看看你是否得到你想要的。


应用委托

//
//  AppDelegate.swift
//  NewProject
//
//  Created by Don Mag on 8/6/19.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)

        let navigationController = UINavigationController();
        let mainViewController = ViewController();

        // don't try to create login view here...
        //let loginViewController = LoginViewController();
        //navigationController.viewControllers = [ mainViewController, loginViewController ];

        // set only your first controller
        navigationController.viewControllers = [ mainViewController ];

        self.window?.rootViewController = navigationController;
        self.window?.makeKeyAndVisible()

        return true
    }
    func applicationWillResignActive(_ application: UIApplication) {
    }
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
    func applicationWillEnterForeground(_ application: UIApplication) {
    }
    func applicationDidBecomeActive(_ application: UIApplication) {
    }
    func applicationWillTerminate(_ application: UIApplication) {
    }
}

视图控制器

//
//  ViewController.swift
//  NewProject
//
//  Created by Don Mag on 8/7/19.
//

import UIKit

class ViewController: UIViewController {

    let button: UIButton = {
        let button = UIButton(frame: CGRect(x: 120, y: 120, width: 50, height: 50));
        button.setTitle("Login", for: .normal);
        button.layer.borderColor = UIColor.lightGray.cgColor;
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.backgroundColor = UIColor.red;
        button.layer.cornerRadius = 5;

        return button;
    }();

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white;
        view.addSubview(button);


        button.addTarget(self, action: #selector(loginPressed(_sender:)), for: .touchUpInside);
        // Do any additional setup after loading the view.
    }

    @IBAction func loginPressed(_sender: UIButton) {
        print("clicked");
        let loginViewController = LoginViewController();
        self.navigationController?.pushViewController(loginViewController, animated: true);
    }

}

class LoginViewController: UIViewController {

    let emailLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 150, y: 150, width: 150, height: 150));
        label.text = "Email address";
        return label;
    }();

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
        view.addSubview(emailLabel);
        print("this is loginViewController");
    }

}

推荐阅读