Change cell title in TableViewController in another ViewController - swift

I'm trying to change the title in a TableViewController from another ViewController. (see image)
The second ViewController is the one with the 3 cells and the third one is the one with a textfield (inputText in code), a button (changeText) and a label (outputLabel). I would like this app to remember what I put in the text field when I go back to the table view and then back into the ViewController. What happens now is:
- I change the text, hit the button and the label changes.
- I go back to the TableViewController and then I go into the ViewController that I was just in with a changed label
- The label is what it was before...
How can I make the app 'remember' what I put in in the text field and what the label was like? My code (ViewController.swift, I linked the 3rd controller to this file, haven't linked the 2nd controller to anything (yet?)):
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var outputLabel: UILabel!
#IBOutlet weak var inputText: UITextField!
#IBAction func changeText(_ sender: UIButton) {
outputLabel.text = inputText.text
}
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.
}
Thanks in advance!

You can reference your ViewController in first controller (TableViewController),
make public inputText
#IBOutlet public weak var inputText: UITextField!
and in viewDidAppear get your text
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let text = ViewControllerVar.inputText.text //your text
}

Related

Button in view doesn't work from view container

I have a view container in my view controller, which is stretched to whole screen. User interaction enabled is on. When I open another view via this view container in this view controller, button is clickable but doesn't respond. What should I do to make it work?
ViewController:
class ViewController: UIViewController {
#IBOutlet weak var viewContainer: UIView!
var views: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
views = [UIView]()
views.append(LoginVC().view)
views.append(RegisterVC().view)
for v in views{
viewContainer.addSubview(v)
}
viewContainer.bringSubview(toFront: views[1])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func switchViewAction(_ sender: UISegmentedControl) {
print("clicked - segment")
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
and this is the button in the another view
#IBAction func registerTapped(_ sender: UIButton) {
print("Clicked - register")
}
I used Managing View Controllers tutorial to make it working.
You have to replace the entire ViewController, not just the View.
#IBAction func switchViewAction(_ sender: UISegmentedControl) {
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
Your current code is just replacing LoginVC's view with RegisterVC's view, instead of replacing LoginVC with RegisterVC. When you do that, you leave behind the ViewController in charge of actually responding to any interaction.
You'll have to rework your code to keep track of the ViewControllers instead of just their views. Then, instead of adding all of your views to viewContainer and moving them to the front when you need to change the current view, you should set the your child ViewController (the ViewController embedded in your container) to the ViewController you want to show (either LoginVC or RegisterVC).

My scrollViewDidScroll function is not receiving calls

I'm trying to check if a user is scrolling up and down on my app but it's not calling my scrollViewDidScroll method
Any ideas why it's not printing received scroll when I scroll up and down on the app?
import UIKit
class CreateAccount: UIViewController, UITextFieldDelegate, UIScrollViewDelegate {
#IBOutlet weak var scrollViewer: UIScrollView!
#IBOutlet weak var usernameTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var reenterPasswordTextField: UITextField!
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.
}
func scrollViewDidScroll(scrollView: UIScrollView) {
print("received scroll")
}
}
Add the scrollview delegate. Whenever you implement a delegate method you need tell it what controller to use, usually it will be self. It's caught me out a few times.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
scrollViewer.delegate = self
}
You need to set delegate of scrollview also don't use tag to differentiate 2 scrollview instead of that create 2 outlets of scrollview and use that in UIScrollViewDelegate methods like this.
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == scrollView1 {
print("received scrollview 1")
}
else if scrollView == scrollView2 {
print("received scrollview 2")
}
}
Delegate are given properly even if your scrollview delegate method not calling then please check you delegate method which you have added for scrollview, this may give warning due to using old swift version scrollview delegate with swift 4.0
Please correct it with below if this is the case:
Old Method:
func scrollViewDidScroll(scrollView: UIScrollView) {
}
New Method:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
}
You can set the .delegate = self as Sarcoma suggests, or:
CTRL drag from scrollView in storyboard onto the File Owner (it is located in the view hierarchy list, actually it is right by the yellow circle icon by the name of your controller: [O] Create Account and choose
delegate

Can't pass data between view controllers using segues (Xcode 7) (Swift)

I have been struggling to pass data between view controllers using segues. I have watched many youtube video tutorials and searched around on forums but I can't figure out why it won't work. I don't get any error messages but when I click the 'Button' the simulator crashes. I would love some help! Thanks!
Here is the code :
ViewController1 :
import UIKit
class ViewController: UIViewController {
#IBOutlet var textField: 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.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController : ViewController2 = segue.destinationViewController as! ViewController2
destViewController.labelText = textField.text!
}
}
ViewController2 :
import Foundation
import UIKit
class ViewController2 : UIViewController {
#IBOutlet var Label: UILabel!
var labelText : String = ""
override func viewDidLoad() {
Label.text = labelText
}
}
StoryBoard Image :
storyboard image
As stated on the comment the problem was that you were using a push segue on a UIViewController.
Please note that push and modal segues are deprecated... Please take a look at this post entry and let me know if you have any doubts
What's the difference between all the Selection Segues?
Make sure to enter some text in your label textField before tapping the button since you are passing that label's text to next scene by using force unwrap.
destViewController.labelText = textField.text! //here compiler expects that your textField has the value
I made a sample project using your same code and didn't get an error. There's nothing wrong with your code.
Try this: Go to your storyboard and check outlets inspector for each view controller and make sure you don't have an outlet to nowhere.

How would I go about adding a uiview vertically above a uiview within an inputAccessoryView?

I'm trying to add a uiview containing multiple buttons, above my current input accessory view. My current input accessory view is a growing textField (like iOS standard Text Message app).
class ViewController: UIViewController {
let textInputBar = ALTextInputBar()
// The magic sauce
// This is how we attach the input bar to the keyboard
override var inputAccessoryView: UIView? {
get {
return textInputBar
}
}
// Another ingredient in the magic sauce
override func canBecomeFirstResponder() -> Bool {
return true
}
}
Example of what I'm trying to do, the app (Facebook Messenger) has a growing textfield or textinput, and in this case, an bar of buttons bellow.
My current view, as mentioned earlier.
try this code
class ViewController: UIViewController {
#IBOutlet weak var myTextField: UITextField!
var textInputBar:UIView = ALTextInputBar()// if it is a uiview type
override func viewDidLoad() {
super.viewDidLoad()
myTextField.inputAccessoryView = textInputBar
}
}

Getting nil value on textview and button

Hello im am making an app where the user can press a button and a random link shows upp. I have in the textfile tried to name blblbl.delegate = self but when running the code it suddenly says that all of my textview and buttons has the value of nil.
class ViewController: UIViewController, UITextViewDelegate,
NSObjectProtocol {
#IBOutlet weak var firstbutton: UIButton!
#IBOutlet weak var firsttextview: UITextView!
let links = ["http://www.google.com","http://www.yahoo.com",
"http://www.discovery.com"]
var currentLinkIndex : Int = 0
override func viewDidLoad() {
Firsttextview.delegate = self
configureTextView()
super.viewDidLoad()
}
func configureTextView() {
func textView(firsttextview: UITextView!, shouldInteractWithURL
URL:
NSURL, inRange characterRange: NSRange) -> Bool {
let url = NSURL(string:links[currentLinkIndex])
UIApplication.sharedApplication().openURL(url!)
return false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func firstaction(sender: UIButton) {
let random : Int = Int(arc4random()) % 3
Firsttextview.text = links[random]
currentLinkIndex = random
}
}
How do a solve this problem? Does i some how have to give the textview and button a value! Thanks in advance!
I guess you need to change :
Firsttextview.delegate to firsttextview.delegate and
Firsttextview.text = links[random] to firsttextview.text = links[random]
I tried to recreate your example by :
Creating a button in the storyboard
Creating the textview in the storyboard
Create + drag and drop to my ViewController Class the IBOutlet firstbutton from the button in the storyboard
Create + drag and drop to my ViewController Class the IBOutlet firsttextview from the textview in the storyboard
Create + drag and drop to my ViewController Class the IBAction firstaction from the button in the storyboard
If the button and the label are created in the storyboard, they don't need to be initialized in your code.
By doing this, your code worked for me.