I have a label1 in a view controller of main.storyboard
i have another label2 in view controller of storyboard named Second.storyboard
how can I pass data from one storyboard to another all solutions I can find are about passing data within the same storyboard.Help me with my question.
make a Global variable in Second View Controller
var labelText = ""
While Pushing to secondViewController from First Controller
let secondVC = secondViewController()
secondVC.labelText = label.text // Pass label text from first VC
self.navigationController?.pushViewController(secondVC, animated: true)
Create the instance of Storyboard with the relevant name while creating the instance of SecondViewController in FirsViewController, i.e
class FirsViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
func pushVC2(_ sender: UIButton) {
if let controller = UIStoryboard(name: "Storyboard-2", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController { //here....
controller.str = "Your_Text_Here"
self.navigationController?.pushViewController(controller, animated: true)
}
}
}
Your SecondViewController looks like,
class SecondViewController: UIViewController {
#IBOutlet weak var label2: UILabel!
var str: String?
override func viewDidLoad() {
super.viewDidLoad()
self.label2.text = str
}
}
Related
import UIKit
//EventList ViewController
class EventPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
class EventForm: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//IBOutlets
#IBOutlet weak var eventNameField: UITextField!
#IBOutlet weak var fromTimePicker: UIDatePicker!
#IBOutlet weak var toTimePicker: UIDatePicker!
#IBOutlet weak var colorPreview: UIView!
#IBAction func cancel(_ sender: Any) {
//empty text field
eventNameField.text = ""
}
#IBAction func save(_ sender: Any) {
if (eventNameField.hasText) {
//fix error handling
eventNameField.backgroundColor = UIColor.systemGray2
//pull data from fields
let text = eventNameField.text!
let fromTime = fromTimePicker.date
let toTime = toTimePicker.date
//initialize object
let currentEvent = EventModel(eventName: text, fromTime: fromTime, toTime: toTime, color: storedColor)
//append to data model
EventDataArray.append(currentEvent)
//transition
present(EventPage(), animated:true)
}
else {
eventNameField.backgroundColor = UIColor.systemRed
}
}
}
I currently have an EventPage class declared as type UIViewController, but upon pressing the save button with a populated text field a transition to a blank ViewController occurs. I've attached the class to the correct ViewController in main.storyboard.
The problem in here is that you are creating a new EventPage but it doesn't inherit from Storyboard.
1
Go to the inspector in your storyboard, select your View Controller, and write an identifier for your View Controller (can be anything)
Write it in Storyboard ID:
2
Replace
present(EventPage(), animated:true)
With
(don't forget to replace 'MYIDENTIFIER' with the id you entered earlier)
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MYIDENTIFIER") as! EventPage
// If you need to do any configurations to your view controller, do that in here.
// For example:
// viewController.label.text = "Hello, world!"
present(viewController, animated:true)
Note
If the name of your Storyboard file name is not called Main, replace "Main" in step 2 with the name of your storyboard file (excluding .storyboard)
In first view controller we have two buttons
if we tap on first view controller oneButn i need to hide onebutnContainerView in secondview controller
if we tap on first view controller secndButn i need to hide twobutnContainerView in secondview controller
in first view controller viewController.oneButnContainerView.isHidden = true getting error:
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
first view controller code:
class firstViewController: UIViewController{
#IBAction func oneButn(_ sender: UIButton) {
self.view.endEditing(true)
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
viewController.delegate = self
viewController.oneButnContainerView.isHidden = true
viewController.twobutnContainerView.isHidden = false
self.navigationController?.pushViewController(viewController, animated: true);
}
#IBAction func secndButn(_ sender: UIButton) {
self.view.endEditing(true)
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
viewController.delegate = self
viewController.oneButnContainerView.isHidden = false
viewController.twobutnContainerView.isHidden = true
self.navigationController?.pushViewController(viewController, animated: true);
}
}
I have outlets for two views in Second view controller
#IBOutlet weak var oneButnContainerView: UIView!
#IBOutlet weak var twoButnContainerView: UIView!
how to hide seconviewcontroller view in firstviewcontroller
That's because you are trying to hide a View that wasn't initialized yet. As a rule of thumb, keep in mind that when you instantiate a viewController, you can only access its data not its views. You have 2 ways of fixing this:
Create 2 variables inside secondViewController:
var isOneButnContainerViewHidden: Bool = false
var isTwoButnContainerViewHidden: Bool = false
Assign a value to those 2 variables inside firstViewController:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "NewZoomAddressViewController") as! NewZoomAddressViewController;
viewController.delegate = self
viewController.isOneButnContainerViewHidden= false
viewController.isTwoButnContainerViewHidden= true
self.navigationController?.pushViewController(viewController, animated: true);
Now inside your secondViewController's viewDidLoad or viewWillAppear, hide/show your buttonContainerViews based on the value of the 2 variables created:
oneButnContainerView.isHidden = isOneButnContainerViewHidden
twoButnContainerView.isHidden = isTwoButnContainerViewHidden
The second way involves forcing the viewController to layout its views by calling loadViewIfNeeded() on the secondViewController before accessing its views (in this case you are trying to hide/show the views).
I am trying to use a tabbar to navigate to other view controllers using Xibs. However, I am not able to find tangible resources to proceed further. My current solution involves mapping and I cannot seem to see where the problem is.
import UIKit
class BottomNavViewController: UIViewController {
#IBOutlet weak var home: UITabBarItem!
#IBOutlet weak var assets: UITabBarItem!
#IBOutlet weak var transactions: UITabBarItem!
#IBOutlet weak var profile: UITabBarItem!
#IBOutlet weak var tabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
navigateTab()
}
func navigateTab() -> Void{
let tabBarController = UITabBarController()
let homeVC = HomeViewController()
let assetsVC = AssetsViewController()
let transactionsVC = TransactionsViewController()
let profileVC = ProfileViewController()
let controllers = [homeVC, assetsVC, transactionsVC, profileVC]
tabBarController.viewControllers = controllers.map {
UINavigationController(rootViewController: $0)
}
}
}
Simply it is like this, You want use UITabbarController
class MYTabViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
let firstVC = FirstVC()
firstVC.tabBarItem = UITabBarItem(title: "Tab 1", image:(your image), tag: 0)
let secondVC = SecondVC()
secondVC.tabBarItem = UITabBarItem(title: "Tab 2", image: (your image), tag: 1)
viewControllers = [firstVC,secondVC]
}
}
How can i get image to some imageView, which is in another Viewcontroller, and wasn't created(is nil) yet
let vc = SelectedCellViewController()
vc.nameLabel.text = MainViewController.dishesArray[indexPath.section].name
Fatal error: Unexpectedly found nil while implicitly unwrapping an
Optional value
Create a variable name and set that variable while creating the instance of SelectedCellViewController. Set the text to nameLabel in viewDidLoad().
class SelectedCellViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
var name: String?
override func viewDidLoad() {
super.viewDidLoad()
self.nameLabel.text = self.name
}
}
Usage:
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SelectedCellViewController") as? SelectedCellViewController {
vc.name = MainViewController.dishesArray[indexPath.section].name
}
I have to code simple program. Just one login screen waiting for some password and after setting proper one, change screen to "program itself" and its features.
I decided to do it by container View and its subviews. everything works fine but delegate which should be responsible for triggering this change doesn't respond. In any option I tried, nothing has changed... it still doesn't respond.
This is code of ViewController (main):
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var container: NSView!
var vc1 : ViewController1 = ViewController1()
var vc2 : ViewController2 = ViewController2()
override func viewDidLoad() {
super.viewDidLoad()
//initialise of delegate:
vc1.delegate = self
// set subviews:
vc1 = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "ViewController1")) as! ViewController1
vc2 = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "ViewController2")) as! ViewController2
self.addChildViewController(vc1)
self.addChildViewController(vc2)
// set first view:
vc1.view.frame = self.container.bounds
self.container.addSubview(vc1.view)
}
func changeViews()
{
for sView in self.container.subviews {
sView.removeFromSuperview()
}
// set second view:
vc2.view.frame = self.container.bounds
self.container.addSubview(vc2.view)
}
}
extension ViewController: ViewController1_Delegate {
func passwdEntered(_ correctPasswd: Bool) {
if correctPasswd == true{
changeViews()
}
}
}
and this is First (SubView) ViewController code, where I'm entering passwd:
import Cocoa
protocol ViewController1_Delegate: class {
func passwdEntered(_ correctPasswd: Bool)
}
class ViewController1: NSViewController{
#IBOutlet weak var passwordField: PasswordField!
weak var delegate: ViewController1_Delegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func passwordFIeldEdited(_ sender: PasswordField) {
delegate?.passwdEntered(true/*for testing purpose always true*/)
}
}
Finally Second (SubView) ViewController code:
import Cocoa
class ViewController2: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
What I'm doing wrong?
First you say this:
var vc1 : ViewController1 = ViewController1()
override func viewDidLoad() {
super.viewDidLoad()
vc1.delegate = self
Then you say this:
vc1 = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"),
bundle: nil).instantiateController(
withIdentifier: NSStoryboard.SceneIdentifier(
rawValue: "ViewController1")) as! ViewController1
So, you set yourself as delegate on a ViewController1 instance, but then, in the very next line, you throw away that ViewController1 and replace it with another!
Move the line v1.delegate = self down to after you set the real value of vc1.