Trying to understand why this crashes when switch changes - ios

learning Swift 3 - not sure why the code to change the background color of my button crashes the app. myButton is a class variable and should be available to the function correct?
class ViewController: UIViewController {
var myButton : UIButton!
var mySwitch : UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
let firstView = UIView()
firstView.backgroundColor = UIColor.white
view = firstView
let myNewView = UIView( frame: CGRect(x:65, y:100, width:300, height:200))
myNewView.backgroundColor = UIColor.blue
//myNewView.layer.cornerRadius = 25
//myNewView.layer.borderWidth = 2
//myNewView.layer.borderColor = UIColor.red.cgColor
self.view.addSubview(myNewView)
let switchDemo=UISwitch();
switchDemo.isOn = true
switchDemo.setOn(true, animated: false);
switchDemo.addTarget(self, action: #selector(ViewController.switchValueDidChange(sender:)), for: .valueChanged);
let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 144, height: 144))
myButton.backgroundColor = UIColor.red
myButton.addTarget(self, action: #selector(ViewController.ratingButtonTapped(sender:)), for: .touchUpInside)
self.view.addSubview(myButton)
myButton.addSubview(switchDemo);
}
func switchValueDidChange( sender:UISwitch){
print("Switch changed")
myButton.backgroundColor = UIColor.green
}
func ratingButtonTapped(sender:UIButton!)
{
print("Button It Works!!!")
}
}

First of all and strictly spoken myButton is an instance variable (actually a property) rather than a class variable.
let myButton = creates a local variable with the same name as the property but it is not the same object.
The solution is to omit let
myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 144, height: 144))

It's simple. You declare a class property but you don't instantiate it. I your viewDidLoad you just instantiate a local variable myButton, instead of instantiating the class property.
Here is a fix:
myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 144, height: 144))
Just remove the let keyword.

You did not instantiate myButton anywhere in your code, therefore it stays nil.
You are, however, declaring a local variable called myButton in viewDidLoad.
You should replace your instantiation with this:
myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 144, height: 144))

Related

UIControl in UITextField.rightView doesnot call actions for controlEvents

I placed a UIControl in a UIView,and set the UIView as the UITextField.rightView,but the UIControl then doesnot call actions for controlEvents I added like:[control addTarget:self action:#selector(searchButtonClicked) forControlEvents:UIControlEventTouchUpInside].If I change the UIControl to a UIButton, it works well, but I has to use UIControl for some reason.Any one know the reason and solution?
I'm using xcode 14.0 on iOS 16.1
import UIKit
class DemoControl: UIControl { }
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(searchBar)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let searchView = UIView(frame: CGRect(x: 0, y: 0, width: demoControl.frame.width, height: demoControl.frame.height))
searchView.addSubview(demoControl)
searchBar.searchTextField.rightViewMode = .always
searchBar.searchTextField.rightView = searchView
}
private lazy var searchBar: UISearchBar = {
let view = UISearchBar(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width - 24, height: 36))
return view
}()
private lazy var demoControl: DemoControl = {
let control = DemoControl(frame: CGRect(x: 0, y: 0, width: 52, height: 24))
control.layer.cornerRadius = 12
control.backgroundColor = .red
control.addTarget(self, action: #selector(controlAction), for: .touchUpInside)
return control
}()
private lazy var demoButton: UIButton = {
let control = UIButton(frame: CGRect(x: 0, y: 0, width: 52, height: 24))
control.layer.cornerRadius = 12
control.backgroundColor = .red
control.addTarget(self, action: #selector(controlAction), for: .touchUpInside)
return control
}()
#objc private func controlAction() {
print("Click!!!")
}
}

UIbutton inside UIViewControllers inside UIScrollView not getting triggered on touch

I'v added two UIViewControllers inside of a UIScrollView, and now I'm trying to add targets to the button inside of the UIViewControllers, I've checked the internet but none of those solutions worked for me, this is what I'm doing
inside of the main ViewController
scrollView.contentSize = CGSize(width: view.frame.width * 2, height:0)
let postsViewController = PostsViewController()
postsViewController.scrollView = scrollView
postsViewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
let chatsViewController = ChatsViewController()
chatsViewController.scrollView = scrollView
chatsViewController.view.frame = CGRect(x: view.frame.width, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(postsViewController.view)
scrollView.addSubview(chatsViewController.view)
postsViewController.didMove(toParent: self)
chatsViewController.didMove(toParent: self)
then inside the postsViewController I'm doing this
let a = UIButton()
a.setImage(currentTheme.chat, for: .normal)
a.imageEdgeInsets = UIEdgeInsets(top: 1.5, left: 0.0, bottom: 1.5, right: 0.0)
view.addSubview(a)
view.addConstraint("V:|-200-[v0(80)]-0-|",a)
view.addConstraint("H:|-200-[v0(80)]-0-|",a)
a.addTarget(self, action: #selector(abc), for: .touchUpInside)
the add "addConstraint" is an extension function just to add constraint to views
the abc function
#objc func abc() {
print("hello")
}
you can see the scrollView setting here
https://ibb.co/ZB1LJsy
Try to initialize your button like this
let a : UIButton = UIButton(type: .custom)
And add this after to the initialize of your UIScrollView
scrollView.delaysContentTouches = false
I tested in an xcode project. I can not execute the function (button) in the other UIViewController.
By cons if you sent the #selector it works.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let scrollView : UIScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.width))
scrollView.delaysContentTouches = false
view.addSubview(scrollView)
let TestViewController = TestViewController1()
TestViewController.select = #selector(demande(sender:))
TestViewController.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(TestViewController.view)
print("dd")
}
#objc func demande(sender: UIButton!) {
print("button click", sender.tag)
}
}
class TestViewController1: UIViewController {
var select : Selector!
override func viewDidLoad() {
super.viewDidLoad()
let button : UIButton = UIButton(type: .custom)
button.setImage(#imageLiteral(resourceName: "turn_off_on_power-512"), for: .normal)
button.tag = 2
button.addTarget(self, action: select, for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
view.addSubview(button)
}
}
Hoping that it helps you
okay, I solved my problem, the reason why the button wasn't getting trigger is because I forgot to attach the viewController of the view to the scrollview, so all I had to do is to attach the viewController as well and it worked.
self.addChild(postsViewController)
self.addChild(chatsViewController)
now it is working.

"Use of Unresolved Identifier" Error when Variable Declared in loadView()

I am trying to create a simple app where when a user taps, the label text will change, as shown in the code below. However, in my function that handles the tap, it says that label is unresolved. I believe this is because the label is loaded in the loadView and cannot be accessed throughout the class (I am a beginner Swift user, so forgive me if I'm wrong.) Here is my code:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
containerView.backgroundColor = UIColor.white
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = "Hey"
containerView.addSubview(label)
self.view = containerView
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
view.addGestureRecognizer(tap)
}
#objc func handleTap(sender:UITapGestureRecognizer) {
label.text = "Changed"
}
}
PlaygroundPage.current.liveView = MyViewController()
Thank you!
You have declared label as a local variable inside loadView - This means it is only accessible in the loadView function. You need it to be a property so that it is accessible throughout your class.
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var label: UILabel!
override func loadView() {
let containerView = UIView (frame: CGRect(x: 0, y: 0, width: 600, height: 600))
containerView.backgroundColor = UIColor.white
self.label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
self.label.center = CGPoint(x: 160, y: 285)
self.label.textAlignment = .center
self.label.text = "Hey"
containerView.addSubview(self.label)
self.view = containerView
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
view.addGestureRecognizer(tap)
}
#objc func handleTap(sender:UITapGestureRecognizer) {
self.label.text = "Changed"
}
}
PlaygroundPage.current.liveView = MyViewController()
You are trying to access label which is declared within loadView() method. handleTap() does not have visibility/access to label.
Declare label as class variable.
Refer here for more information about declaration and scope: https://docs.swift.org/swift-book/ReferenceManual/Declarations.html

Why is my button not working in Swift Playgrounds?

I am trying to make a playground and I have a button that says "Let's play!" and moves into a new view controller.
I looked at the code from this website and put it into my code:
http://lab.dejaworks.com/ios-swift-3-playground-uibutton-action/
This is all of my code (like, all of it):
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
override func loadView() {
//Introduction
let view = UIView()
view.backgroundColor = .red
//title
func labelCool() {
let label = UILabel()
label.frame = CGRect(x: 50, y: 300, width: 400, height: 100)
label.text = "Add-Add - A Wonderful Game!"
//label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.numberOfLines = 3
label.font = UIFont(name: "HelveticaNeue-Bold", size: 30)
UILabel.animate (withDuration: 10.0, animations:{
label.textColor = .black
})
UILabel.animate(withDuration: 5.0, animations:{
label.textColor = .blue
})
view.addSubview(label)
}
labelCool()
//subtitle
let subtitle = UILabel()
subtitle.frame = CGRect(x: 50, y: 400, width: 200, height: 50)
subtitle.text = "Curated and Created by Yours Truly, Adit Dayal!"
subtitle.numberOfLines = 4
self.view = view
view.addSubview(subtitle)
}
override func viewDidLoad() {
class Responder : NSObject {
#objc func action() {
print("Yay!")
}
}
let responder = Responder()
//next page
let button = UIButton(frame : CGRect(x: 0, y: 500, width: 200, height: 50))
button.setTitle("Let's Play!", for: .normal)
button.backgroundColor = .blue
button.addTarget(responder, action: #selector(Responder.action), for: .touchUpInside)
view.addSubview(button)
}
}
class gameViewController: UIViewController {
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
For now, I just want the button to display "Yay!" when clicked, but it is doing nothing!
Does anyone know why? (I'm on a bit of a time constraint)
Thank you so much,
Adit Dayal
Your Responder class is inside the viewDidLoad() function put the class outside like so
class Responder : NSObject {
#objc func action() {
print("Yay!")
}
}
override func viewDidLoad() {
let responder = Responder()
//next page
let button = UIButton(frame : CGRect(x: 0, y: 500, width: 200, height: 50))
button.setTitle("Let's Play!", for: .normal)
button.backgroundColor = .blue
button.addTarget(responder, action: #selector(responder.action), for: .touchUpInside)
view.addSubview(button)
}
The problem is that you are creating the responder object inside the viewDidLoad, as a local variable; this cause the object to be destroyed when the function ends (but we want that object alive even after). You have to retain that object, so instead of creating a local variable, create an instance variable by simply saving it as a class scope:
class Responder : NSObject {
#objc func action() {
print("Yay!")
}
}
let responder = Responder() // this is now outside the viewDidLoad, so it's an instance variable
override func viewDidLoad() {
//next page
let button = UIButton(frame : CGRect(x: 0, y: 500, width: 200, height: 50))
button.setTitle("Let's Play!", for: .normal)
button.backgroundColor = .blue
button.addTarget(responder, action: #selector(Responder.action), for: .touchUpInside)
view.addSubview(button)
}

Swift Content Tabs?

I am building an app and I am required to have 3 tabs if you click on the first tab, its a tableview, second tab a different tableview, third tab, TextView, here is an image of what I am trying to do:
Every time I google looking for examples of something like this, I get referred to UITabBarController, I don't think a tab bar is what I am looking for.
What you are looking for is called Segmented Control (UISegmentedControl).
You can see it in Action natively e.g. in both the Apple iTunes and Health App.
Instead of performing segues to different views with the UITabBarController, you can, using Interface Builder, connect it to your UIViewController like so
#IBOutlet weak var segmentedControl: UISegmentedControl!
and in
viewDidLoad() {
switch segmentedControl.selectedIndex {
case 0: // Do something on your first picture
someFunction()
case 1: // Do something on your second picture
performSegue(withIdentifier: "your identifier here", sender: nil)
case 2: // Do something on your third picture
image.isHidden = true
button.isEnabled = false
default: break
}
... etc. You could also initiate different View Controllers instead of manipulating just one View, which you can then access via segues.
Please see official Swift doc # https://developer.apple.com/documentation/uikit/uisegmentedcontrol?changes=_3
I would create this layout programmatically. You want a custom look to it so I do not think the standard controls are your best option.
Here is a playground to get you started on one way this could be achieved:
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
let view1 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
let view2 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
let view3 = UIView(frame: CGRect(x: 0, y: 100, width: 768, height: 924))
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x: 0, y: 0, width: 768, height: 1024)
self.view.backgroundColor = .black
// View to have a border around the buttons
let box = UIView(frame: CGRect(x: 40, y: 20, width: 300, height: 50))
box.layer.borderColor = UIColor.white.cgColor
box.layer.borderWidth = 2
self.view.addSubview(box)
// Tab buttons
let button1 = UIButton(frame: CGRect(x: 40, y: 20, width: 100, height: 50))
button1.setTitle("Orange", for: .normal)
button1.tag = 1
button1.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
self.view.addSubview(button1)
let button2 = UIButton(frame: CGRect(x: 140, y: 20, width: 100, height: 50))
button2.setTitle("Blue", for: .normal)
button2.tag = 2
button2.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
self.view.addSubview(button2)
let button3 = UIButton(frame: CGRect(x: 240, y: 20, width: 100, height: 50))
button3.setTitle("Green", for: .normal)
button3.tag = 3
button3.addTarget(self, action: #selector(tabTouched(sender:)), for: .touchUpInside)
self.view.addSubview(button3)
// Tab Views
view1.backgroundColor = .orange
self.view.addSubview(view1)
view2.backgroundColor = .blue
view2.alpha = 0
self.view.addSubview(view2)
view3.backgroundColor = .green
view3.alpha = 0
self.view.addSubview(view3)
}
// When each of the buttons are tapped we will hide or show the correct tab's view
#objc func tabTouched(sender: UIButton) {
if sender.tag == 1 {
UIView.animate(withDuration: 0.3) {
self.view1.alpha = 1
self.view2.alpha = 0
self.view3.alpha = 0
}
} else if sender.tag == 2 {
UIView.animate(withDuration: 0.3) {
self.view1.alpha = 0
self.view2.alpha = 1
self.view3.alpha = 0
}
} else if sender.tag == 3 {
UIView.animate(withDuration: 0.3) {
self.view1.alpha = 0
self.view2.alpha = 0
self.view3.alpha = 1
}
}
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = ViewController()
You can copy and paste this into an empty playground to see it in action. Essentially this is showing or hiding the appropriate views based on which button is selected. You can swap out the views for view controllers if you so desire.

Resources