Xcode: instantiating a view controller with custom variable - ios

So I want to instantiate a view controller from storyboard and change its static variables.
This is "vc1" - the view controller to be instantiated:
import UIKit
class vc1: UIViewController {
#IBOutlet weak var lbl_title: UILabel!
static var title = "initial value"
override func viewDidLoad() {
super.viewDidLoad()
lbl_title.text = vc1.title
}
}
And this is my root vc.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btn_go: UIButton!
#IBAction func btn_gogogo(_ sender: Any) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vc1") as! vc1
vc.title = "bla"
self.present(vc, animated: true, completion: nil)
}
}
Here I'm trying to change the static variable of the view controller that I just instantiated,
with no effect. The variable ( in my case 'title' ) is always stuck to its initial value.
What is the problem here?
Best
Mark

Don't try to override the view controller's title property. Instead, create your own:
class vc1: UIViewController {
#IBOutlet weak var lbl_title: UILabel!
var myTitle = "initial value"
override func viewDidLoad() {
super.viewDidLoad()
lbl_title.text = myTitle
}
}
class ViewController: UIViewController {
#IBOutlet weak var btn_go: UIButton!
#IBAction func btn_gogogo(_ sender: Any) {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vc1") as! vc1
vc.myTitle = "bla"
self.present(vc, animated: true, completion: nil)
}
}

Related

Loading a UIViewController into a Container View programmatically

This is in the latest XCode, 11.3. I've looked through the answers to similar questions, but some of the function names and parameters seem to have changed.
While working through a Udemy course, I'm trying to make a simple log in / sign up interface. In my first view controller I have a vertical stack view containing a label, a segmented control, and a container view. The segmented control can be either "Log In" or "Sign Up". I want to load the appropriate view controller when the segmented control changes.
My LogInViewController has a vertical stack view with a text field for username, another for password, and a Log In button. My SignUpViewController has a vertical stack view with text fields for email address, username, password, and password confirmation followed by a Sign Up button.
My first view controller looks like this:
class ViewController: UIViewController
{
#IBOutlet weak var logInSignUpControl: UISegmentedControl!
#IBOutlet var customContainer: UIView!
var logInVC: LogInViewController?
var signUpVC: SignUpViewController?
var activeVC = 0
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
initializeCustomControllers()
}
func initializeCustomControllers()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
logInVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as? LogInViewController
signUpVC = storyboard.instantiateViewController(withIdentifier: "SignUpViewController") as? SignUpViewController
logInVC?.willMove(toParent: self)
logInVC?.view.frame = customContainer.bounds
customContainer.addSubview(logInVC!.view)
addChild(logInVC!)
logInVC?.didMove(toParent: self)
}
Unfortunately, this only shows the LogInViewController, not the label or segmented control.
I haven't been able to find a clear description of how to do this in the latest version of XCode and Swift, so I've pieced this together from various blog posts and Stackoverflow comments about older versions. Any help is greatly appreciated.
Try the below code :)
PS: #IBAction func controlChanged(_ sender: Any) is an IBAction for the "ValueChanged" Event
class ViewController: UIViewController {
#IBOutlet weak var logInSignUpControl: UISegmentedControl!
#IBOutlet var customContainer: UIView!
var logInVC: LogInViewController?
var signUpVC: SignUpViewController?
var activeVC = 0
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
initializeCustomControllers()
}
#IBAction func controlChanged(_ sender: Any) {
changeVC()
}
func changeVC() {
if activeVC == 0 {
signUpVC?.view.removeFromSuperview()
logInVC?.willMove(toParent: self)
logInVC?.view.frame = customContainer.bounds
customContainer.addSubview(logInVC!.view)
addChild(logInVC!)
logInVC?.didMove(toParent: self)
} else {
logInVC?.view.removeFromSuperview()
signUpVC?.willMove(toParent: self)
signUpVC?.view.frame = customContainer.bounds
customContainer.addSubview(signUpVC!.view)
addChild(signUpVC!)
signUpVC?.didMove(toParent: self)
}
activeVC = activeVC == 0 ? 1 : 0
}
func initializeCustomControllers()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
logInVC = storyboard.instantiateViewController(withIdentifier: "LogInViewController") as? LogInViewController
signUpVC = storyboard.instantiateViewController(withIdentifier: "SignUpViewController") as? SignUpViewController
changeVC()
}
}
Make sure this #IBOutlet var customContainer: UIView! is linked to the containerview and not to the vc's main view in storyboard

How can I show a modal screen in swift5?

I am currently working on the iOS project.
My problem is that when I see the new screen, I don't see the old screen and I only see the new one.
I want a modal screen. What am I missing?
move ModalScreen
func callAlert(_ text: String!) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
myAlert.text = text
self.present(myAlert, animated: false, completion: nil)
}
modalScreenController
class ModalViewController : UIViewController {
var text: String?
#IBOutlet weak var modalCustomAlert: UITextView!
#IBOutlet weak var okButton: UIButton!
#IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}
...
modalStoryBoard
You need to set a background color with alpha component in your viewcontroller's view.
class ModalViewController : UIViewController {
var text: String?
#IBOutlet weak var modalCustomAlert: UITextView!
#IBOutlet weak var okButton: UIButton!
#IBAction func okPress(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Your desired color and alpha component
modalCustomAlert.layer.cornerRadius = 8
self.modalCustomAlert.text = text
}

how can I manage controllers in Container View with using tab bar

On my storyboard I have main ViewController, not TabBarViewController, which consist of TabBar on the bottom, view on the top and ContainerView on the middle. ContainerView have a NavigationController. I also have 4 ViewControllers, one of them - RootViewController of NavigationController. I wish to show one of ViewControllers when I selecting TabBarItem, and in future I will add slide menu, which also will show selected ViewController.
I have next code, which only shows initial ViewController inside ContainerView, and when I selecting TabBarItems, new ViewControllers don't showing and I see only first View Controller. What goes wrong?
class ViewController: UIViewController {
#IBOutlet weak var container: UIView!
#IBOutlet weak var first: UITabBarItem!
#IBOutlet weak var second: UITabBarItem!
#IBOutlet weak var third: UITabBarItem!
#IBOutlet weak var fours: UITabBarItem!
#IBOutlet weak var tabBar: UITabBar!
var firstVC: FirstViewController?
var secondVC: SecondViewController?
var thirdVC: ThirdViewController?
var foursVC: FoursViewController?
var navi: UINavigationController?
override func viewDidLoad() {
super.viewDidLoad()
tabBar.delegate = self
initialSetup()
}
func initialSetup() {
tabBar.selectedItem = tabBar.items?.first
navi = self.storyboard?.instantiateViewController(withIdentifier: "containerNavi") as? UINavigationController
firstVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
thirdVC = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController
foursVC = self.storyboard?.instantiateViewController(withIdentifier: "FoursViewController") as? FoursViewController
}
func showVC(number: Int) {
switch number {
case 0:
navi?.popToRootViewController(animated: true)
print("0")
case 1:
if let second = secondVC {
navi?.pushViewController(second, animated: true)
}
print("1")
case 2:
if let third = thirdVC {
navi?.pushViewController(third, animated: true)
}
print("2")
case 3:
if let fours = foursVC {
navi?.pushViewController(fours, animated: true)
}
print("3")
default:
return
}
}
}
extension ViewController: UITabBarDelegate {
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
showVC(number: item.tag)
}
}
Storyboard screenshot:
You can try to use this extesion to add/remove any of the 4 to containerView
extension UIViewController {
func add(_ child: UIViewController, frame: CGRect? = nil) {
addChildViewController(child)
if let frame = frame {
child.view.frame = frame
}
view.addSubview(child.view)
child.didMove(toParentViewController: self)
}
func remove() {
willMove(toParentViewController: nil)
view.removeFromSuperview()
removeFromParentViewController()
}
}
// use it like this
let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")
self.add(vc, frame: self.containerView.frame)
to remove
vc.remove()

I am trying to using a custom delegate in swift but when I want to call its methods, it did not get called

I have been searching for how the delegate works and I tried to do it in my project. Unfortunately, the delegate method I implement does not get called ever. I am trying to do a slide-out navigation panel. so what I did is that I put two uicontainerviews, one is for slide-out navigation panel and the other for main view controller
enter image description here
The code is that
For main view controller
protocol MainViewControllerDelegate {
func toggleSideMenu()
}
class MainViewController: UIViewController {
var delegate: MainViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Slide Action
#IBAction func slideMenuTapped(_ sender: UIBarButtonItem){
delegate?.toggleSideMenu()
print("Slide Menu has been tapped")
}
}
For container view controller
class ContainerVC: UIViewController {
#IBOutlet weak var SideMenuConstraint: NSLayoutConstraint!
#IBOutlet weak var slideMenuContainer: UIView!
#IBOutlet weak var mainViewContainer: UIView!
var mainViewController: MainViewController?
var isSideMenuOpened = false
override func viewDidLoad() {
super.viewDidLoad()
mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self
}
}
extension ContainerVC: MainViewControllerDelegate{
func toggleSideMenu() {
print("It works")
if isSideMenuOpened{
isSideMenuOpened = false
SideMenuConstraint.constant = -260
mainViewContainer.layer.shadowOpacity = 0
} else {
isSideMenuOpened = true
SideMenuConstraint.constant = 0
mainViewContainer.layer.shadowOpacity = 0.59
}
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
}
extension UIStoryboard{
static func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
static func mainViewController() -> MainViewController? {
return mainStoryboard().instantiateViewController(withIdentifier: "MainViewController") as? MainViewController
}
}
Please let know what's wrong
I think the reason is that you embed your main view controller in navigation controller :
let navigationController = self.childViewControllers.last as! UINavigationController
let mainViewController = navigationController.topViewController as! MainViewController
mainViewController?.delegate = self
Here is where you got wrong:
mainViewController = UIStoryboard.mainViewController()
mainViewController?.delegate = self
this mainViewController is not the same as the child of the container view controller, so setting its delegate doesn't really do anything.
You need to first get the VC that is the child of the container view controller:
mainViewController = self.childViewControllers.last as! MainViewController
mainViewController.delegate = self

Swift iOS -How to pop to different SegmentControl Index using a NavigationController

//Index=0 //contains buttonToSwitchTab to pop to TabOneVC
TabZeroNavCntrlr ----TabZeroVC --- TabChangeVC
//segmntCntrl /
NavCntlr--ContainerView---
\ //Index=1
TabOneNavCntrlr ---- TabOneVC
I have a NavController that hasContainerView connected to it. Inside the containerView there is a segmentControl. The containerView has 2 more nav controllers connected to it that each lead to their own child controllers: TabZeroVC and TabOneVC. Initially the first view shown is TabZeroVC (selectedSegmentIndex = 0). There is a button inside there that leads to TabChangeVC which has a buttonToSwitchTab inside of it. Pressing that button I want to pop to TabOneVC.
I tried this code below by using my navigation controller to make the switch but on the line tabOneVC.segmentedControl.selectedSegmentIndex = 1 I get a crash: unexpectedly found nil while unwrapping an Optional value
I put a break point on it and the containerVC gets initialized but it's 3 properties segmentedControl, tabZeroVC, and tabOneVC are all coming up as nil. Since segmentedControl is nil that's where the crash is happening.
class TabChangeVC: UIViewController{
#IBAction func buttonToSwitchTab(sender: UIButton){
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let containerVC = mainStoryboard.instantiateViewControllerWithIdentifier("ContainerViewController") as! ContainerViewController
containerVC.segmentedControl.selectedSegmentIndex = 1 //crash happens here
self.navigationController?.popToViewController(containerVC, animated: true)
}
}
ContainerView:
class ContainerViewController: UIViewController{
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var tabZeroVC: UIView!
#IBOutlet weak var tabOneVC: UIView!
...
}
TabOneVC is selectedSegmentIndex = 1
My question is how do I pop to TabOneVC from the TabChangeVC and more importantly why is the containerVC's segmentedControl coming up as nil?
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil
let tabOneVC = mainStoryboard.instantiateViewControllerWithIdentifier("TabOneNavigationController") //TabOneNavCntrlr identifire
tabOneVC.segmentedControl.selectedSegmentIndex = 1
appDelegate.window!.rootViewController = tabOneVC
I don't know why the segmentedControl is coming up as nil but I was able to pop to tabOneVC by using NSNotificationCenter. I created a func named tabSwitch() with the segment I wanted to show and then I added a listener inside the viewDidLoad of the ContainerViewController:
class ContainerViewController: UIViewController{
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var tabZeroVC: UIView!
#IBOutlet weak var tabOneVC: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(tabSwitch), name: "tabSwitch", object: nil)
}
func tabSwitch(){
self.segmentedControl.selectedSegmentIndex = 1
self.tabZeroVC.hidden = true
self.tabOneVC.hidden = false
}
}
Inside the TabChangeVC I added a notifier to trigger it and navigationController?.popToRootViewControllerAnimated(true) to get back to the root vc:
class TabChangeVC: UIViewController{
#IBAction func buttonToSwitchTab(sender: UIButton){
NSNotificationCenter.defaultCenter().postNotificationName("tabSwitch", object: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
}

Resources