ios - 为什么 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 中?为什么 uielement 已在视图控制器之间共享?
注意:我尝试在谷歌中搜索。也许因为我是一名网络开发人员,我可能没有用正确的词进行搜索。
解决方案
运行您发布的确切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
Main
从Main 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");
}
}
推荐阅读
- android - Koin 在模块之间共享实例
- java - 从millis开始错误的int day
- javascript - 如何在 react-native 的实验性 WindowedListView 中实现滚动恢复
- mysql - MySQL LOAD DATA LOCAL INFILE 避免重复
- node.js - 如何在 Google Home Hub 中启用音乐操作的下一个按钮
- javascript - 排序后输入和文本区域的数据延迟
- java - OSGi 'immediate = true' 组件未激活
- javascript - this.state.display &&
- php - 如何自动将当前日期时间添加到我的数据库中
- python - 测试时如何为 OneToOneField 传递数据?