How to identify variables with the same identifier in different subViews - swift3

First post and I'll be honest, I'm pretty bad at coding. I'm working with Swift 3 for a school project and I have a question.
I'm creating multiple subviews through a for in loop through an array...
...and each created subview contains one programmatically created label, each with a different word, and a programmatically created UI Tap Gesture attached to each view...
Import UIKit
class ViewController: UIViewController, UIGestureRecongizerDelegate
{
var array = ["Each","One","Has","Its","Own","Name"]
var label = UILabel()
var DynamicView = UIView()
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
createSubView()
//end SuperView
}
func createSubview()
{
for thing in array {
var string = array[count]
count = count + 1
DynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
DynamicView.center = CGPoint(x: xV,y: yV)
DynamicView.backgroundColor = UIColor.gray
self.view.addSubview(DynamicView)
label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = "\(string)"
self.DynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action:
#selector(ViewController.tapped(recignizer:)))
DynamicView.isUserInteractionEnabled = true
DynamicView.addGestureRecognizer(tapGesture)
}
}
func tapped (recignizer:UITapGestureRecognizer){
print("\(label.text!)")
}
}
... (I'm excluding the code that separates all the views from being stacked on top of each other, so please assume that these views are spaced out, and this list of views is in a scroll view in my actual code, just not cramming that spaghetti code in here)...
...The result of the code is a layout of subViews, each with its own label that is a different word, and a tap gesture for each subView which triggers a function...
...What I want is a way to tap on each subview and find what the text in the label is. As of right now, when you tap on a view, you get what the label.text was in the last View made, so the result of print("label.text!") --> 'Name' , which makes some sense.
Is there any possible way to find the specific labels text even though they are all called "Label"?
I'm not looking to do a collection view or anything of the sort, I need something of this sort of coding to get what I need done.
Thank you to whoever helped this foolish swift student, sorry that my Swift lingo is hot garbage like my code.

Your problem is you are initializing the same DynamicView and label inside the for loop so it will overwrite and it will have last value from the array. To solved the problem you need to create separate UIView and separate UILabel in for loop. Also you need to set userInteraction of label to true because by default its false.
for thing in array {
let dynamicView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 160))
dynamicView.center = CGPoint(x: xV,y: yV)
dynamicView.backgroundColor = UIColor.gray
self.view.addSubview(dynamicView)
let label = UILabel(frame: CGRect(x: 25, y: 25, width: 100, height: 21))
label.center = CGPoint(x: 60, y: 13)
label.textAlignment = .center
label.font = UIFont(name: "Arial", size: 13)
label.text = thing
self.dynamicView.addSubview(label)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapped(recignizer:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tapGesture)
}
Now your all view’s origin is CGPoint.zero so only last view label will visible and when you tap on label to get text of its you can get this way.
func tapped(recignizer:UITapGestureRecognizer){
if let tappedLabel = recignizer.view as? UILabel {
print("\(tappedLabel.text)”)
}
}

Related

how to add textfield as a custom view in leftBarButtonItems, here textfield need to be capable to typing

how to add a new view that view having textfield and buttons in it and how this view can be added in leftbarbuttonitmes, and textfield need to be capable to typing.
Right now I have added this view to leftbarbuttonitems successfully but my textfield is not capable to type. Is there any way by which we can type if we add in leftbarbuttonitems.
Here it is: Just paste below code in your viewDidLoad method.
let viewLeftButton: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 160.0, height: 32.0))
let textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 100.0, height: viewLeftButton.frame.size.height))
textField.borderStyle = .roundedRect
textField.font = UIFont.systemFont(ofSize: 14.0)
textField.placeholder = "Type here..."
let button: UIButton = UIButton(frame: CGRect(x: textField.frame.size.width + 8, y: 0, width: 44.0, height: viewLeftButton.frame.size.height))
button.setTitle("Click", for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
button.setTitleColor(.blue, for: .normal)
viewLeftButton.addSubview(textField)
viewLeftButton.addSubview(button)
self.navigationItem.hidesBackButton = true
let leftBarButton = UIBarButtonItem(customView: viewLeftButton)
self.navigationItem.leftBarButtonItem = leftBarButton

why subView from another view controller moves as I scroll?

I have a subview from another view controller, whenever my scroll arrives on it, the subview change it's position to x = 0 and y = 0, below my navigation bar, but if I scroll fast and not stop on that view, everything is fine.
the code I use for addSubView is :
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "saleView") as! SaleViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = CGRect(x: 0 , y: 0 , width: saleTopView.frame.width, height: saleTopView.frame.size.height)
saleTopView.addSubview(popOverVC.view)
what I miss? thanks
add
popOverVC.didMove(toParentViewController : self)
at the end. Not a must do, but for simplicity, you could also rewrite
popOverVC.view.frame = self.saleTopView.frame

Add Image Array and text view in scrollView with Swift 3

I would like to add image and texts (under each image view) in ScrollView.
I add ScrollView firstly and add UIImage View. But, I add two images in image array and it only appears one image when I run the app. Here is my code;
#IBOutlet weak var imgView: UIImageView!
var imgArray = [#imageLiteral(resourceName: "home_image"),#imageLiteral(resourceName: "provider_image")]
and in ViewDidLoad() function,
for index in 0 ..< imgArray.count
{
let imgView = UIImageView()
debugPrint(index)
imgView.image = imgArray[index]
}
I also want to add each image caption under each image. How to do it? Why only one image appear? :(
Try this:
let width = customScroll.frame.size.width
for item in 1..<4{
image = UIImageView(frame: CGRect(x: (CGFloat(item) * width - width), y: 10, width: width, height: 250))
image.image = UIImage.init(named: "img"+String(item))
customScroll.addSubview(image)
print(image.frame)
}
customScroll.contentSize = CGSize(width: view.frame.size.width * 4, height: 300)

Apple TV Default Scrolling behaviour for UIScrollView with a UITextView?

Is it possible to have a UIScrollView behave like a normal scroll window as seen on say a website? Meaning not scrolling by focusing on different UIViews inside the scroll view. I have a long UITextView that expands at a variable height and I would like the user to scroll normally and read the text.
But adding a UITextView to a UIScrollView (Both with a given contentSize) does not give me any results.
As implemented:
//:: Scroll View
scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 1920, height: 1080))
scrollView.autoresizingMask = [.flexibleHeight]
scrollView.translatesAutoresizingMaskIntoConstraints = true
scrollView.backgroundColor = UIColor.red
scrollView.addSubview(txtView)
view.addSubview(scrollView)
//:: Anchor
txtView.translatesAutoresizingMaskIntoConstraints = false;
txtView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120).isActive = true
txtView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
txtView.widthAnchor.constraint(equalToConstant: 825).isActive = true
txtView.contentSize = CGSize(width: 825, height: 2000)
txtView.clipsToBounds = false
scrollView.isScrollEnabled = true
scrollView.showsVerticalScrollIndicator = true
scrollView.contentSize = CGSize(width: 1920, height: 2000)
Was as easy as removing the UIScrollView and enable scrolling on the UITextView:
txtView.isScrollEnabled = true
Surprised no one answered that.

Swift: Public Skscene not presenting with filenamed?

Ok, I am working in Swift playgrounds here and cannot figure out how to present an SkScene that is another class. So far I have placed this SKScene file, GameScene.swift, in my Sources folder and made it public.
In my main playground file I have tried:
import PlaygroundSupport
import SpriteKit
import XCPlayground
import Foundation
let frame = CGRect(x: 0, y: 0, width: 500, height: 600) //view size
let view = SKView(frame: frame)
let scene = GameScene(fileNamed: "GameScene")
//scene.backgroundColor = NSColor.blue
view.presentScene(scene)
view.showsFPS = true;
PlaygroundPage.current.liveView = view
Along with any other answer I could find on stack overflow. Nothing is working - the playground timeline is just a blank grey and this is output:
Is this impossible?
Yes it i possible, but at the moment your Gamscene object is nil. This is because GameScene(filenamed: "" ) only works when you have the scene builder. Wish your not using in Xcode.
Instead. Use
GameScene(size: CGSize)
and make sure you create the game scene with
class GameScene : SKScene {
override sceneDidLoad() {
super.sceneDidLoad()
}
}
after that present your scene from a view
let sceneSize = CGSize(width: 250, height: 550)
let scene = GameScene(size: sceneSize)
let viewRect = CGRect(x: 0, y: 0, width: 250, height: 550)
let skView = SKView(frame: viewRect)
skView.presentScene(scene)
PlaygroundPage.current.liveView = skView
Thats an example to get you started ^^