iOS 11.3 - performSegue from ContainerViewController from parentViewController - ios

I have a parent view (with ViewController) which has a sidebar menu and containerView(ContainerViewController) in it. at the containerView, I have 2 segue link with their own view and viewController (Screen1 with Screen1ViewController, Screen2 with Screen2ViewController).
I have buttons to launch screen1 and screen2 from ViewController but somehow the setting performSegue() function seems like not firing.
ViewController
import UIKit
class ViewController: UIViewController {
var container : ContainerViewController!
#IBOutlet var SideBar: UIView!
#IBOutlet weak var viewConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
container = self.storyboard?.instantiateViewController(withIdentifier: "ContainerViewController") as! ContainerViewController
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "tocontainer"{
self.container = segue.destination as! ContainerViewController
}
}
#IBAction func launchScreen1(_ sender: Any) {
container.performSegue(withIdentifier: "toscreen1", sender: self)
}
#IBAction func launchScreen2(_ sender: Any) {
container.performSegue(withIdentifier: "toscreen1", sender: nil)
}
}
ContainerViewController
import UIKit
class ContainerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func launchScreen2() {
print("launchscreen2")
self.performSegue(withIdentifier: "toscreen1", sender: self)
}
}
As you can see i even tried a function launchScreen2 for the button to call. the print message did appear in console but somehow no effect on the self.performSegue.
I believe is the way I prepare the segue is wrong, I just don't know what else to try.

Related

How can I do this in Swift? (My version don't working)

I have problem with join navigator controller and containerView.
I want if I click button in ViewController, the view in TabViewController must change on other view. (I know, haven't code to change view, but i want first print this log.) TabBarViewController is embedded in ContainerView. TabViewController is rootView NavigationController.
So I created this:
And this is not working, when click a button I don't see text "this is test" in log.
This is my swift code:
import UIKit
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("this is test");
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func click1(_ sender: Any) {
navigationController?.performSegue(withIdentifier: "segue1", sender: self)
}
}
If I got your question right, you want to change the index of your TabBarController on click of the button in ViewController. Correct me if i am wrong.
Give the segue connecting your ViewController and TabBarController an identifier.
Then in your ViewController, declare this
var yourTabBarController : UITabBarController?
Then in your prepareForSegue method of ViewController,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourTabBarControllerSegue" {
if let tabBarController = segue.destination as? UITabBarController {
self.yourTabBarController = tabBarController
}
}
}
and lastly, on button press:
#IBAction func yourButtonPressed(_ sender : Any) {
if yourTabBarController?.selectedIndex == 0 {
yourTabBarController?.selectedIndex = 1
} else {
yourTabBarController?.selectedIndex = 0
}
}
The ViewController you are trying to performSegue doesn't seem to have a navigationController. Try performSegue without the navigationController, like this:
#IBAction func click1(_ sender: Any) {
performSegue(withIdentifier: "segue1", sender: nil)
}

How to pass data from ViewController3 to ViewController1

I am new to Swift and was wondering on how to pass data from ViewController to ViewController that isn’t connected by segue. I have spend 2 hours on this and still can’t figure a way to do it.
Edit: The problem that I am confused on is How to take out all the stacked ViewController expect the root ViewController while being able to pass data back to the first ViewController from ViewController3 with
'navigationController?.popToRootViewController(animated: false)'
class ViewController: UIViewController, nameDelegate {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var textfield1: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "Name"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc2 = segue.destination as! ViewController2
vc2.middleOfTransferingName = textfield1.text!
}
#IBAction func buttonPressed1(_ sender: Any) {
performSegue(withIdentifier: "go2", sender: self)
}
func nameThatWasEntered(name: String) {
label1.text = name
}
}
class ViewController2: UIViewController, nameDelegate {
var middleOfTransferingName: String = ""
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc3 = segue.destination as! ViewController3
vc3.transferedWithSegueLabelText = middleOfTransferingName
vc3.namePassBack = self
}
#IBAction func buttonPressed2(_ sender: Any) {
performSegue(withIdentifier: "go3", sender: self)
}
func nameThatWasEntered(name: String) {
}
}
protocol nameDelegate {
func nameThatWasEntered(name: String)
}
class ViewController3: UIViewController {
#IBOutlet weak var label3: UILabel!
#IBOutlet weak var textfield3: UITextField!
var transferedWithSegueLabelText: String = ""
var namePassBack: nameDelegate?
override func viewDidLoad() {
super.viewDidLoad()
label3.text = transferedWithSegueLabelText
}
#IBAction func buttonPressed3(_ sender: Any) {
namePassBack!.nameThatWasEntered(name: textfield3.text!)
navigationController?.popToRootViewController(animated: false)
}
}
I found the way to transfer ViewController3 data to ViewController!
var vc3 = ViewController3()
then making ViewController as the delegate of vc3 upon loading.
override func viewDidLoad() {
super.viewDidLoad()
vc3.namePassBack = self
}
Then going back to ViewController3, I add ""vc3."" before using the delegate.
#IBAction func buttonPressed3(_ sender: Any) {
vc3.namePassBack!.nameThatWasEntered(name: textfield3.text!)
navigationController?.popToRootViewController(animated: false)
}
if you use segue then use 'Unwind segue'
for this write given code in your 'A view controller'
#IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}
In B_viewcontroller, On press button call 'Unwind segue'.
Note:- With the help of Unwind segue you can send the value for 3ed vc to 1se vc same as you send value form 1st vc to 2nd vc
for more go to this link

Change Label text in a ContainerView on click of a button from other class (Uiviewcontroller)

I got small containerView with UILabel on main app screen. I got UIButton on main UIViewController. I want to change text of label that belongs to containerView class by clicking button in UIViewController.
I try to make it with delegation, but for some reason i got a mistake (Unwraping optional)...
I try to make it with Protocol, bud method "addText" in ContView dont works((((((
class ViewController: UIViewController {
var delegate: DelegateProtocol?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button(_ sender: Any) {
delegate?.addText(String2: "123")
}}
///////////////////////////////////////////////////////////////////////
protocol DelegateProtocol {
func addText(String2: String)
}
//////////////////////////////////////////////////////////////////////
class ContViewController: UIViewController, DelegateProtocol {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "con" {
let vc = segue.destination as! ViewController
vc.delegate = self
}
}
func addText(String2: String) {
label.text = String2
}
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
first make an global variable in your view controller
private var viewController: YourVC?
then give your container view segue an identifier and do follwing
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourviewcontroller Segue identifiew" { //your container view segue identifier
viewController = segue.destination as? SelectedImageVC
}
}
now you can use you viewController to access label in your containerview controller like
if let controller = viewController {
controller.yourlabel.text = "text you want"
}

store segued data on a separate viewcontroller (swift4)

My code takes text from a textfield and segues to a separate view controller. The problem is that it can only store 1 segue entry. The new one replace the old every time the button is pressed. How on vc2 can I store every new entry to the segue.
VC
import UIKit
class ViewController: UIViewController {
#IBOutlet var txt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func pressButton(_ sender: Any) {
if txt.text != "" {
performSegue(withIdentifier: "segue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondController = segue.destination as! twoViewController
secondController.myString = txt.text!
}
}
twoVC
import UIKit
class twoViewController: UIViewController {
#IBOutlet var labelSmtih: UILabel!
var myString = String()
override func viewDidLoad() {
super.viewDidLoad()
labelSmtih.text = myString
}
}
If the second view controller is a UISplitViewController for example you should be able to append the text of the button to the label, by doing something in the lines of:
#IBAction func pressButton(_ sender: Any) {
if txt.text != "" {
performSegue(withIdentifier: "segue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondController = segue.destination as! twoViewController
secondController.myString += txt.text!
}
}
If you really have a separated view controller it will probably lose the data once you go back to the first VC. In that case, I suggest you save the current text of the label on viewWillDisappear of the 2nd VC and then retrieve this on the prepareforsegue function on the first VC. More info here: Swift: How to store user preferences?

access textfield text from another viewController

I try to access a textfield content in my view profileView from my first view viewController but it doesn't work.
I read that I have to use override func prepare(for segue: UIStoryboardSegue, sender: Any?) {} so I did but it still not working and I don't understand why.
Here is my viewController code:
// This code is the code generated by xCode, only the loginField was added.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var loginField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
and here is my profileController prepare func:
var login: String!
#IBOutlet weak var myLabel: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare")
if (segue.identifier == "mainMenu")
{
let svc = segue.source as! ViewController
login = svc.loginField.text
self.myLabel.text = login
print(login)
}
}
Actually, the thing i really read was "prepareForSegue" func but when I try to override it, xCode won't compile the code...
Thanks for your help.
write your prepareforSegue method in your viewController
why?
Because you want to send ViewController textfield data to profileViewController.That is write your prepareForSegue in Source. Here,
Source = ViewController
Destination = ProfileView
Note:
Use a variable in your destinationViewController(ProfileView Controller) and keep the textField data in this variable.
like this in prepareForSegue.
destinationVC.someVariable = self.SourceViewLabel.text // your sourceViewController
and in viewDidLoad of your destinationViewController(ProfileView) write this.
self.profileLabel.text = somVariable
In my first view i wrote this.
#IBAction func nexVCAction(_ sender: Any) {
self.performSegue(withIdentifier: "secondvc", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondvc" {
let vc = segue.destination as! secondVC
vc.stringHolder = textField.text!
}
}
And this is the second view code.
import UIKit
class secondVC: UIViewController {
#IBOutlet weak var textlabel: UILabel!
var stringHolder:String = ""
override func viewDidLoad() {
super.viewDidLoad()
textlabel.text = stringHolder
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I made git project for this.
Check only FirstVC.swift and secondVC.swift
And this is the output.
You cannot set the text of the UITextField and any other components of UIKit in the prepare for segue, because all View components of the recently allocated controller are not initialised yet they are all nil in prepareForSegue. they allocated only be when the viewController is presented.
You set Text for current ViewController TextField at prepareForSegue. Instead of this you should set text at viewWillAppear method while pop or dismiss the presented ViewController by using Notification, Delegate or userDefaults etc...
You just need a small fix since UITextField and any other components of UIKit are not initialised yet in the prepare for segue:
var login: String!
#IBOutlet weak var myLabel: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare")
if (segue.identifier == "mainMenu")
{
let svc = segue.source as! ViewController
login = svc.loginField.text
// self.myLabel.text = login
print(login)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.myLabel.text = login
}
Data flow from one controller to another is the very popular theme for ios beginners. There are several approaches to achieve this goal: segues, delegate pattern, notifications, initialization of new instance of controller, saving data to local storage etc.
You should user prepare(for segue: sender:) method only when you have segue from you current controller to another. You can get access to destination controller instance via destination property of segue parameter.
Also never call IBOutlet properties of destination controller in prepareForSegue - it can be cause of crash, because Outlets may be uninitilized at this time. Instead use String property for example.
Here is sample code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "mainMenu") {
if let destinationVC = segue.destination as? ViewController {
destinationVC.login = self.myLabel.text
}
}
}
Where login in destinationVC is the String variable to store login.

Resources