My scrollViewDidScroll function is not receiving calls - swift

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

Related

scroll view content offset non responsive in viewDidLoad

scrollView.contentOffset does work properly inside button func but the same code is non responsive in the viewDidLoad func
#IBOutlet weak var scrollView: UIScrollView!
#IBAction func scrollBtn(_ sender: UIButton) {
scrollView.contentOffset.x = 200
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentOffset.x = 200
}
i am trying to change the initial position of the scroll view
Try using it in viewWillAppear or viewDidAppear. May be the reason behind it is not working in viewDidLoad is that the view has not get rendered at the time the viewDidLoad invoked.

Unable to perform segue from UIView connected to XIB

This is UIView file's owner of XIB
import UIKit
class sideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let mapviewcontroller = MapViewController()
#IBAction func userProfile(_ sender: UIButton) {
mapviewcontroller.performSegueFromView(stringFromView: "userprofile")
}
Whereas inside my UIviewcontroller
class MapViewController: UIViewController
{
func performSegueFromView(stringFromView:String){
performSegue(withIdentifier: "\(stringFromView)", sender: nil)
}
}
It says that my segue identifier does not exist, please help, i'm quite lost. And i couldn't perform segue in UIView as well, and i tried to use storyboard ID method too, but the present(vc,animated:true,completion:nil) not showing up.
First things first, I think you should not access the view controller from your view. It is better to model the controller logic in the view controller rather than in the view.
That said, you can use a delegate pattern to implement your functionality.
import UIKit
class SideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let delegate: SideBarContentDelegate? = nil
#IBAction func userProfile(_ sender: UIButton) {
self.delegate?.performSegueFromView(withIdentifier: "userprofile")
}
}
protocol SideBarContentDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class MapViewController: UIViewController, SideBarContentDelegate {
#IBOutlet private weak var sidebarView: SideBarContent!
override func viewDidLoad() {
super.viewDidLoad()
self.sidebarView.delegate = self
}
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
}
More info : https://stackoverflow.com/a/1340470/2603900

Swift 4: scrollViewDidScroll not working

I created first View controller "MainViewController" and put into it Container View with outlet contentContainer.
I created second View Controller "AboutContentViewController" and put into it ScrollView
I connect Container View and AboutContentViewController programmatically:
class MainViewController: UIViewController {
#IBOutlet weak var contentContainer: UIView!
override func viewDidLoad() {
let contentControllerName = "AboutContentViewController"
if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
contentContainer.addSubview(contentController.view)
}
}
}
I tried to catch scrollViewDidScroll event using this code
class AboutContentViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var topGalleryScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
topGalleryScrollView.contentSize = CGSize(width: 3750.0, height: 417.0)
topGalleryScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrollViewDidScroll called")
}
}
In this context scrollViewDidScroll event not working
I have solved problem. AboutContentViewController is need to be add as child view controller to MainVewController before adding its's view to container:
class MainViewController: UIViewController {
#IBOutlet weak var contentContainer: UIView!
override func viewDidLoad() {
let contentControllerName = "AboutContentViewController"
if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
addChildViewController(contentController)
contentContainer.addSubview(contentController.view)
}
}
}

Change cell title in TableViewController in another ViewController

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
}

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
}
}