How can swift closure reference properties of class its running from? - ios

I have the following 2 controllers listed below. I'm using delegation to try and create a progressWindow which will run code and print it nicely but where the code is arbitrary.
The closures are defined by the class conforming to the protocol (in my case SyncViewController), but I want to change the UI of the progressWindowViewController from SyncViewControllers codeToRun {} closure. How do I do this?
SyncViewController.swift
import UIKit
class SyncViewController: UIViewController, progressWindowDelegate {
var codeToRun = {
//(self as! ProgressWindowViewController).theTextView.text = "changed the text"
print("code to run")
}
var codeToCancel = {print("code to cancel")}
var titleToGive = "Starting Sync..."
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func yesSyncButtonAction(sender: UIButton) {
//Segue to the ProgressWindowViewController...
}
#IBAction func noSyncActionButton(sender: UIButton) {
tabBarController?.selectedIndex = 1 //assume back to inventory section
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "SyncToProgressSegue"){
let progressWindow = segue.destinationViewController as! ProgressWindowViewController
progressWindow.controllerDelegate = self //sets the delegate so we have reference to this window still.
}
}
}
ProgressWindowViewController.swift
import UIKit
protocol progressWindowDelegate{
var titleToGive : String {get}
var codeToRun : ()->() {get}
var codeToCancel : ()->() {get}
}
class ProgressWindowViewController: UIViewController {
#IBOutlet weak var theTextView: UITextView!
#IBOutlet weak var theProgressBar: UIProgressView!
#IBOutlet weak var navItemLabel: UINavigationItem!
//Sets delegate
var controllerDelegate:progressWindowDelegate!
override func viewDidLoad() {
super.viewDidLoad()
navItemLabel.title! = controllerDelegate.titleToGive
dispatch_async(dispatch_get_main_queue(),{
self.controllerDelegate.codeToRun() //Will run code accordingly.
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func cancelNavItemButtonAction(sender: UIBarButtonItem) {
dispatch_async(dispatch_get_main_queue(),{
self.controllerDelegate.codeToCancel()
})
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
An example of how his might be used is downloading thousands of inventory records with images, which would print the inventory details as it grabs them into the progressWindow.
But this progressWindow could also be used for other large/small tasks that need to print particular stuff into the progressWindow textarea (like logging in and therefore coming from a different view controller than sync in my example). The idea is to make it a dynamic class.

Instead of creating a variable, just use a function/method?
override func viewDidLoad() {
super.viewDidLoad()
dispatch_async(dispatch_get_main_queue(),{
self.controllerDelegate?.codeToRun(self)
})
}
.
protocol progressWindowDelegate : class {
var titleToGive : String {get}
func codeToRun(progressWindowViewController:ProgressWindowViewController)
var codeToCancel : ()->() {get}
}
class SyncViewController: UIViewController, progressWindowDelegate {
func codeToRun(progressWindowViewController:ProgressWindowViewController) {
print("code to run")
}
Also make delegate weak and optional:
delegate weak var controllerDelegate:progressWindowDelegate? = nil

Related

Passing Data into an Array- Swift

I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
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.
}
#IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
#IBOutlet weak var AddPlayerTextField: UITextField!
#IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
#IBOutlet weak var DisplayArray: UILabel!
#IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}

Sending data to another controller - Without going to him

I want to send data to another controller without opening it.
Example
Main controller:
override func viewDidLoad() {
let vc = SecondViewController()
vc.test = "ABCDFER"
}
Second controller:
var test: String
override func viewDidLoad() {
print(test)
}
How to do it?
It works for me this way
class ViewController: UIViewController {
var otherViewController: OtherViewController!
override func viewDidLoad() {
super.viewDidLoad()
otherViewController = OtherViewController()
otherViewController.test = "ABCDFER"
}
#IBAction func press() {
self.show(self.otherViewController, sender: nil)
}
}
class OtherViewController: UIViewController {
var test: String!
override func viewDidLoad() {
super.viewDidLoad()
print(test)
}
}
In your Main controller, as soon as viewDidLoad() finishes your instance of SecondViewController is destroyed / deallocated. If you want to set a value inSecondViewController at that point, so you can "use" it later, you need to keep a reference to that instance:
So, in Main controller:
var secondVC: SecondViewController?
override func viewDidLoad() {
secondVC = SecondViewController()
secondVC.test = "ABCDFER"
}
Now, later - perhaps on a button tap - you want to use that same instance:
#IBAction func buttonTap(_ sender: Any) {
print("test in secondVC:", secondVC?.test)
}
Keep in mind the view life cycle, if the view viewDidLoad() it's only executed when loading the view through a xib or when view related actions are done with the controller, like addSubview().
The value is being passed and will not be deallocated while your main controller is alive.
You can force a lifecycle event to be called, but isn't recommended at all.
class ViewController: UIViewController {
var otherViewController: OtherViewController!
override func viewDidLoad() {
super.viewDidLoad()
otherViewController = OtherViewController()
otherViewController.test = "ABCDFER"
//Do not do this
otherViewController.viewDidLoad()
}
}
class OtherViewController: UIViewController {
var test: String!
override func viewDidLoad() {
super.viewDidLoad()
print(test)
}
}

UIscrollview delegation

I am trying to pass data between my two view controllers in my UIscrollview. I am trying to use delegation to send data between Viewcontroller1 and Viewcontroller2. The delegate is Viewcontroller, while the delegator is Viewcontroller1 and Viewcontroller2.
In the code posted below, when the switch in Viewcontroller1 is toggled, it makes the switch in Viewcontroller2 put to the "off" state. I keep on getting the
fatal error: unexpectedly found nil while unwrapping an Optional value
error when I run it, but I have no clue what is causing this problem. Any ideas why?
Below is the Viewcontroller that contains the Uiscrollview and the subviews/childviews
import UIKit
class ViewController: UIViewController, testing {
var vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
var vc2 = ViewController2(nibName: "ViewController2", bundle: nil)
#IBOutlet weak var scrollView: UIScrollView!
func test1() {
vc2.switch2.on = false
}
override func viewDidLoad() {
super.viewDidLoad()
self.addChildViewController(vc1)
self.scrollView.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)
var frame1 = vc2.view.frame
frame1.origin.x = self.view.frame.size.width
vc2.view.frame = frame1
self.addChildViewController(vc2)
self.scrollView.addSubview(vc2.view)
vc2.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
here is the Viewcontoller1 code
protocol testing{
func test1()
}
class ViewController1: UIViewController {
var delegate:testing?
#IBOutlet weak var switch1: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
let vc = ViewController()
self.delegate = vc
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func switch1toggled(sender: AnyObject) {
delegate?.test1()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
and here is the Viewcontroller 2 code
import UIKit
class ViewController2: UIViewController {
#IBOutlet weak var switch2: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func switch2toggled(sender: AnyObject) {
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
sorry for the long post, I have been stuck for a week on how to change the state of another switch from toggling a switch in another class, and this was the most efficient way that I found
Try This:
ViewController1
class ViewController1: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
let switch1Key = "view1Switch"
override func viewWillAppear(animated: Bool) {
view1Switch.on = defaults.boolForKey(switch1Key)
}
#IBOutlet weak var view1Switch: UISwitch!
#IBAction func view1SwitchChanged(sender: UISwitch) {
defaults.setBool(view1Switch.on, forKey: switch1Key)
}
}
ViewController2
class ViewController2: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
let switch1Key = "view1Switch"
override func viewWillAppear(animated: Bool) {
view2Switch.on = defaults.boolForKey(switch1Key)
}
#IBOutlet weak var view2Switch: UISwitch!
#IBAction func view2SwitchChanged(sender: UISwitch) {
defaults.setBool(view2Switch.on, forKey: switch1Key)
}
}
This method syncs the state of the two UISwitches using viewWillAppear and NSUserdefaults. The basic thought pattern is that you save the state of the switch to NSUserdefaults so that when either ViewController1 or ViewController2 is instantiated the view1Switch or view2Switch outlet's .on property is set to the saved value.
Caveats:
The first value for the switch when ViewController1 is instantiated (in the first app run) will be off because boolForKey returns false when there is no saved value. This can be hacked by using view1Switch.on = true directly after view1Switch.on = defaults.boolForKey(switch1Key)
This method makes the switches have the same value. In order to make them have different values, you can use a ! operator like so in ViewController2 view2Switch.on = !defaults.boolForKey(switch1Key). This way switch 1 will always be the opposite of switch 2.
I recommend this method over delegation because, while delegation is powerful, its power doesn't seem needed here.
If you have any questions please ask! :D

ViewControllerApple.Apple.Type does not have a member named appleQuantity

I have a UITextField named 'appleQuantity', when I try to convert it to an int to do math with it, it gives me an error named 'ViewControllerApple.Apple.Type does not have a member named appleQuantity'
Here's the code, the error is in 'var b:Int=appleQuantity.text.toInt()'
import UIKit
class ViewControllerApple: UIViewController {
//80, 0, 0, 0, 22, 0, 2, 20, 2, 2
class Apple {
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
var b:Int=appleQuantity.text.toInt()
// var appleCal = 80 * b
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
I'm Really new to swift, so the more details provided, the more helpful it is!
Thanks so much in advance!
UPDATED Answer:
I feel that you're thinking of things the wrong way slightly. Here is how I propose you try and achieve what you're doing.
import UIKit
class ViewControllerApple: UIViewController {
class Apple {
var b: Int?
init() {
}
func getAppleCalculation() -> Int {
if let unwrappedB = b {
return unwrappedB * 80
}
else {
return 0
}
}
}
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
// This is where the view has loaded. Your buttons and textviews will
// have loaded and you can perform any actions on them.
var apple = Apple()
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
apple.b = unwrappedAppleQuantityInt
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
OLD ANSWER: Your variable b will actually never contain anything except the default value.
You should assign your Int b in your class' viewDidLoad, or following your structure, in your object's init function:
class Apple {
#IBOutlet weak var appleQuantity: UITextField!
#IBAction func addApple(sender: UIButton) {
}
var b: Int
init() {
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
b = unwrappedAppleQuantityInt
}
else {
// assign b a default value
b = 0
}
}
}
or you could do return a computed value, provided you only care about the output rather than storing the value, like so:
var b:Int {
get {
if let unwrappedAppleQuantityInt = appleQuantity.text.toInt() {
return unwrappedAppleQuantityInt
}
else {
return 0
}
}
}

Changing label text inside method

I'm using delegation to pass back information from a view controller.
This is the method
func writeValueBack(value: String) {
self.label.text = value
}
The function gets called and all is great apart from the label doesn't update.
The label has a value and is not returning nil, I checked with this line
println(self.label.text)
It prints the value of 'value'
So that means that the label's text is being set to 'value' but it's not updating.
I even tried using the main thread but no luck
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
I just don't know what the problem is.
Any help would be great.
Protocol:
protocol writeValueBackDelegate {
func writeValueBack(value: String)
}
EDIT:
Code for my view controller:
//
// ViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class ViewController: UIViewController, writeValueBackDelegate {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "MainSegue" {
var vc = segue.destinationViewController as SecondViewController
vc.labelText = textField.text
vc.delegate = self
}
}
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
#IBAction func button(sender: AnyObject) {
self.label.text = textField.text
}
#IBAction func segue(sender: AnyObject) {
performSegueWithIdentifier("MainSegue", sender: self)
}
}
Second ViewController source:
//
// SecondViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
var labelText: String!
var delegate: writeValueBackDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func button(sender: AnyObject) {
label.text = textField.text
}
#IBAction func segueBack(sender: AnyObject) {
var editedText = label.text
performSegueWithIdentifier("SecondSegue", sender: self)
if (delegate != nil) {
delegate?.writeValueBack(editedText)
println("working")
}
}
}
The issue is in your second view controller, which as rdelmar pointed out creates a new instance of your first view controller instead of navigating back to the original instance.
To fix this, you could use dismissViewControllerAnimated:completion instead of performing your second segue. But an unwind segue would lead to simpler code, and can be achieved by adding this to your first view controller:
#IBAction func unwindFromSecond(unwindSegue: UIStoryboardSegue) {
if let secondViewController = unwindSegue.sourceViewController as? SecondViewController {
label.text = secondViewController.label.text
// Or whatever you need to retrieve data from the second controller
}
}
Then in your storyboard create an unwind segue from the second view controller. For example if you have a Dismiss button, control-drag from this button to the Exit icon in your second view controller scene and choosing unwindFromSecond. For detailed steps see the answer to this other question: What are Unwind segues for and how do you use them?
You can now remove the writeValueBackDelegate declaration and associated variables, the writeValueBack method and the second view controller's segueBack method

Resources