ios - 如何在 Swift (Xcode) 上通过代码显示屏幕?

标签 ios iphone swift xcode uiviewcontroller

我是 Xcode 的新手,我正在尝试为 iOS 构建一个应用程序。该应用程序必须有多个屏幕( View Controller ),但是当我尝试从第一个屏幕显示第二个屏幕时遇到了麻烦。

我想在单击特定按钮时显示第二个屏幕(这是典型的登录屏幕),因此当用户成功登录时,必须显示第二个屏幕。该按钮在“ViewController.swift”中有这段代码

let username: String = txtUser.text!
let password: String = txtPassword.text!
var message: String
if(username=="" || password=="") {
  message = "You haven't introduced your username/password"
} else {
  message = "Welcome, \(username)"
}
let alert = UIAlertController(title:"Login", message:message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title:"ok", style: UIAlertActionStyle.default, handler:nil)

我试图通过右键单击按钮并拖动到第二个屏幕来在“Main.storyboard”中添加“Show action”,但是当我运行该应用程序时,当我单击按钮:

2018-08-21 12:54:57.886357+0200 App[1370:33542] [MC] Lazy loading NSBundle MobileCoreServices.framework
2018-08-21 12:54:57.888305+0200 App[1370:33542] [MC] Loaded MobileCoreServices.framework
2018-08-21 12:55:00.959900+0200 App[1370:33542] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/erd/Library/Developer/CoreSimulator/Devices/CD00D9C1-1400-4E0E-8CA9-4D46B6EF352E/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-08-21 12:55:00.961220+0200 App[1370:33542] [MC] Reading from private effective user settings.
2018-08-21 12:55:05.137241+0200 App[1370:33542] <UIView: 0x7fbc4dd14060; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x6040002322e0>>'s window is not equal to <UIAlertController: 0x7fbc4e070c00>'s view's window!
2018-08-21 12:55:27.262709+0200 App[1370:33542] <UIView: 0x7fbc4dd14060; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x6040002322e0>>'s window is not equal to <UIAlertController: 0x7fbc4e938a00>'s view's window!
2018-08-21 12:55:41.425141+0200 App[1370:33542] <UIView: 0x7fbc4dd14060; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x6040002322e0>>'s window is not equal to <UIAlertController: 0x7fbc4e017000>'s view's window!

但是,我尝试通过一个新按钮来完成它并且效果很好(没有上述错误)

有没有办法通过代码显示副屏?如果是,这对我来说会更容易,因为我只需要在用户输入正确的用户名+密码时显示该屏幕。

提前致谢!

最佳答案

您可以使用以下定义的几种方式中的任何一种:

  1. 您可以通过Segue 执行此操作。请引用以下内容: http://www.codingexplorer.com/segue-swift-view-controllers/

  2. 使用Push 到第二个 ViewController

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as SecondViewController 
navigationController?.pushViewController(vc, animated: true)
  1. 使用 Present 到第二个 ViewController
let secondView = self.storyboard?.instantiateViewControllerWithIdentifier("secondViewPageID") as! SecondViewController
self.presentViewController(secondView, animated: true, completion: nil)

关于ios - 如何在 Swift (Xcode) 上通过代码显示屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51947739/

相关文章:

ios - 将手势检测添加到可重用 UITableCellView 中使用的自定义 UIView 的正确方法是什么?

Swift闭包赋予变量功能

ios - Apple Push Notification Service 发送重复通知

iphone - ios中的过滤器数组检查多个属性

ios - 使用 CAKeyframeAnimation 沿 UIBezierPath 移动 UIImageView 不会执行任何操作

ios - 在自定义键盘上需要帮助

objective-c - NSPredicate 与 FUNCTION 不起作用

ios - UICollectionView 无法滚动

ios - 在 lowerCamelCase 中命名变量时的奇怪行为

iphone - iOS,退出后台时重启动画