Reputation:
I'm new to Xcode and I'm trying to build an app for iOS. The app must have several screens (View Controller), but I'm having a trouble when I try to show the second one from the first one.
I want to show the second one when I click on a specific button (it's the typical login screen), so when the user logins successfully, the second screen must be shown. The button has this code in "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)
I've tried to add the "Show action" in the "Main.storyboard" by right-clicking on the button and drag into the second screen, but when I run the app I have this error when I click on the button:
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!
However, I've tried to do it through a new button and it worked well (without the error above)
Is there a way to show the second screen through code? If yes, it would be easier for me because I just need to show that screen when the user enters the correct username+password.
Thank you in advance!
Upvotes: 0
Views: 1456
Reputation: 332
You can use any one out of several ways defined below:
You can do this via Segue. Please refer this for the segue: http://www.codingexplorer.com/segue-swift-view-controllers/
Using Push to second ViewController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as SecondViewController
navigationController?.pushViewController(vc, animated: true)
let secondView = self.storyboard?.instantiateViewControllerWithIdentifier("secondViewPageID") as! SecondViewController
self.presentViewController(secondView, animated: true, completion: nil)
Upvotes: 1
Reputation: 451
Swift 3 & 4
Firstly you add a Navigation controller to Initial View Controller. then add these line in any Button Action.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
//Make sure You set Your ViewController Identifier
let objectVC = mainStoryboard.instantiateViewController(withIdentifier: "SubcategoryListVC") as! SubcategoryListVC
self.navigationController?.pushViewController(objectVC, animated: true)
Upvotes: 0