When I run my code below within my view controller
import UIKit
class SecondViewController: UIViewController {
let fifthVC = self.storyboard?.instantiateViewController(withIdentifier: "fifthVC") as? FifthViewController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func twentyLife() {
fifthVC.lifePointsInt = 20
}
#IBAction func thirtyLife() {
fifthVC.lifePointsInt = 30
}
#IBAction func fortyLife() {
fifthVC.lifePointsInt = 50
}
}
I am getting error
Value of type '(SecondViewController) -> () -> SecondViewController'
has no member 'storyboard'
How can I fix this?
I don't know what you are trying to do here but to remove that error , you should call the following line inside some function
let fifthVC = self.storyboard?.instantiateViewController(withIdentifier: "fifthVC") as? FifthViewController
You can either call it in viewDidLoad or you can make a separate function and call it. But first you need to define fifthVC variable in view controller.
var fifthVC:UIViewController?
Then inside viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
fifthVC = self.storyboard?.instantiateViewController(withIdentifier: "fifthVC") as? FifthViewController
}
Related
I am a beginner in Swift, and I do not yet understand all the elements.
I am trying to execute a function present in a ViewController (ProjectTabBarController) from another ViewController (ProjectInfosViewController). I end up with an error when I execute the function from the second.
For the context, it is for a navigation button of 3 UIViewController belonging to a UITabBarViewController, itself embedded in a UINavigationController
Thank you in advance ! (Sorry for my bad English)
import UIKit
//MARK:-TAB CONTROLLER
class ProjectTabBarController: UITabBarController {
#IBOutlet weak var ui_saveButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func saveAction(_ sender: Any) {
// code
disableSaveButton() // ALL IT'S FINE HERE
}
func enableSaveButton() {
ui_saveButton.title = "Save"
ui_saveButton.isEnabled = true
}
func disableSaveButton() {
ui_saveButton.title = "Saved"
ui_saveButton.isEnabled = false
} }
//MARK:-PROJECT INFORMATIONS
class ProjectInfosViewController: UIViewController, UITextFieldDelegate {
let superController = ProjectTabBarController()
override func viewDidLoad() {
super.viewDidLoad()
}
func textFieldDidChangeSelection(_ textField: UITextField) {
superController.enableSaveButton() // BUT HERE, DOESN'T
} }
//MARK:-PROJECT FIXTURES
class ProjectFixturesViewController: UIViewController, UITableViewDataSource {
}
//MARK:-PROJECT CONTACT
class ProjectContactViewController: UIViewController {
}
This
let superController = ProjectTabBarController()
is a new vc that's not presented , if the vc is inside the tabController then do
let res = self.tabBarController as! ProjectTabBarController
res.......// call what you need
The variable latitude in ViewController1 is visible. Why is the variable from another ViewController empty? Whenever I run the code the .text property of ActualCoordinatesText label is empty...
class ViewControllerGpsMaps: UIViewController {
#IBOutlet weak var ActualCoordinatesText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func GetCoordinates(_ sender: Any) {
GetActualCoordinates()
}
public func GetActualCoordinates() {
let sb = storyboard?.instantiateViewController(withIdentifier: "ViewController1") as! ViewController
ActualCoordinatesText.text = sb.latitude
}
}
Thanks for the help!
You are recreating a new instance of ViewController1 that means that the data included is the initialized value...
If your ViewControllerGPSMaps is called by VieController1 you should use the prepare(for segue:, sender:) of the ViewController1 to "give" the data you want to transfer...
I'm having an issue with Delegating. I'm relatively new to the concept, and but conceptually I get it and it's importance. I'm just having trouble using it. I can't seem to pass data from one class to the other. I know there are existing examples of delegation out there on stack overflow but they aren't quite capturing my misunderstanding. I get the use of protocols, delegation, and calling it in a class. I believe there just some small nuance that I'm missing... And it's visible in the lack of functionality in my code
//my protocol:
protocol StingHolder {
func StringPasser(ThisText text: String)
}
Creating the delegate protocol relation, places data to be passed then dismisses the View Controller
// my classes for placing data to be passed
class changeLabel: UIViewController,UITextFieldDelegate{
var Delegate: StingHolder?
#IBOutlet weak var TexrBeingPassed: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
TexrBeingPassed.delegate = self
// Do any additional setup after loading the view.
}
#IBAction func ButtonPassingDataOtherView(_ sender: Any) {
Delegate?.StringPasser(ThisText: TexrBeingPassed.text!)
dismiss(animated: true, completion: nil)
}
}
Creates an instance of the change lable class and its delegate and sets itself to be the delegate *supposedly changes the label, but It doesn't
///class to receive data
class ViewController: UIViewController{
#IBOutlet weak var LableName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
#IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
}
inherits the protocol and tells it to change the label to whatever the changelabel class delegate has passes
// extension view controller inheriting the protocol
extension ViewController : StingHolder{
func StringPasser(ThisText text: String){
print("Delegate is working")
LableName.text = text
///
}
}
I want the one view controller to edit the text label of another view controller
The object which you have self as a delegate of, is not the same object presented on the screen.
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" is set...
var lableChange = changeLabel()
lableChange.Delegate = self
// Do any additional setup after loading the view.
}
#IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
// but "ViewController" is presented
var ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView")
self.present(ViewController, animated: true, completion: nil)
}
labelChange and ViewController are two different, independent objects. One created by calling init directly, and the other created by calling storyBoard.instantiateViewController. You should set the delegate of the latter instead:
override func viewDidLoad() {
super.viewDidLoad()
// "labelChange.delegate" can be deleted
}
#IBAction func EditController(_ sender: Any) {
var storyBoard = UIStoryboard(name: "Test", bundle: nil)
if let ViewController = storyBoard.instantiateViewController(withIdentifier: "TestView") as? labelChange {
ViewController.delegate = self
self.present(ViewController, animated: true, completion: nil)
}
}
Im trying to send an array "ranking" of Struct between two VC after tapping the "rankingButton". I don't know what to code in the second vc to get and manipulate the array.
First VC:
struct TopPoint{
let user: String
let points: Int
var position: Int
}
var ranking: [TopPoint] = []
override func viewDidLoad() {
super.viewDidLoad()
let user1 = TopPoint(user: "fran", points: 1324, position: 1)
ranking.append(user1)
}
#IBAction func rankingButton(_ sender: Any) {
let vc = TopUserTableViewController(nibName: "TopUserTableViewController", bundle: nil)
vc.ranking = ranking
navigationController?.pushViewController(vc, animated: true)
}
Second VC:
class TopUserTableViewController: UITableViewController {
var ranking = [TopPoint]()
override func viewDidLoad() {
super.viewDidLoad()
}
//what to code to get the array?
}
Once your secondVC pushed, you can directly access the array in the secondVC. Because you set its ranking property equal to ranking in the firstVC For example consider having a method to iterate through array elements and print user of each struct element inside your array. You can do it by:
class TopUserTableViewController: UITableViewController {
var ranking: [TopPoint]!
override func viewDidLoad() {
super.viewDidLoad()
iterateElements()
}
func iterateElements() {
ranking.forEach { (element) in
print(element.user)
}
}
}
You could also access directly inside your viewDidLoad, I added iterateElements method as reference, access directly in viewDidLoad as:
override func viewDidLoad() {
super.viewDidLoad()
ranking.forEach { (element) in
print(element.user)
}
}
viewDidLoad() is too early in the lifecycle. Just move your code:
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ranking.forEach { (element) in print(element.user) } }
}
I would like to increase the value of the property in the first interface controller in the IBAction (Add1) method of the second interface controller
and then use the value of this property to update the label when the first view controller is activated
I was able to increase the value , but the value increases even if I press the back button.
I need to find a solution so that when I press the IBAction in the second interface controller , I can get that result and use it in the first interface controller and update the label.
here is the code:
First interface controller:
Blockquote
#IBOutlet weak var resultButtonLabel: WKInterfaceButton!
#IBAction func resultButton() {
pushControllerWithName("secondInterfaceController", context: self)
}
override func willActivate() {
super.willActivate()
resultButtonLabel.setTitle("\(counter++)")
}
Blockquote
Second interface controller:
Blockquote
var counter = 1
#IBAction func weScored() {
counter++
popController()
}
Blockquote
The easy way to implement this idea is use of NSUserDefaults
In your firstViewController you can read values form NSUserDefaults this way:
override func viewDidLoad() {
super.viewDidLoad()
let score = NSUserDefaults().integerForKey("Score")
resultButtonLabel.text = "\(score)"
}
and into your SecondViewController you can increase this counter with NSUserDefaults this way:
import UIKit
class SecondViewController: UIViewController {
var counter = Int()
override func viewDidLoad() {
super.viewDidLoad()
counter = NSUserDefaults().integerForKey("Score")
}
#IBAction func weScored(sender: AnyObject) {
counter++
NSUserDefaults().setInteger(counter, forKey: "Score")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
}
Hope this will help.