If we click on the AddCommentsButton the AddCommentsTextView must enable. how to implement in swift iOS? - swift

#IBOutlet weak var AddCommentsButton: UIButton!
#IBOutlet weak var AddCommentsTextView: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.AddCommentsButton.setTitle("Add Comments", for: UIControl.State.normal)
self.AddCommentsButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
AddCommentsTextView.rchDelegate = self as? RCHTextViewDelegate
AddCommentsTextView.delegate = self as? UITextViewDelegate
AddCommentsTextView.placeholder = "Leave a comment"
AddCommentsTextView.canShowBorder = true
AddCommentsTextView.addBorderWithCornerRadius(cornerRadius: 0, borderColor: .Gray, borderWidth: 0.5)
AddCommentsTextView.backgroundColor = UIColor.clear
self.AddCommentsTextView.font = UIFont.dynamicFont(control: .listKeyText)
self.AddCommentsTextView.textColor = UIColor.listKeyTextColor()
}
#IBAction func didTapAddCommentsButton(_ sender: Any) {
}

If you are looking for when you click on didTapAddCommentsButton button then the AddCommentsTextView Textview should show the cursor in the textview for entering the text then you can use:
#IBAction func didTapAddCommentsButton(_ sender: Any) {
self.AddCommentsTextView.becomeFirstResponder()
}
So that on button click it will enable textview cursor for entering purpose.
UPDATED:
If you want to remove focus from the text entry then use:
self.AddCommentsTextView.resignFirstResponder()

Related

Change textcolor with click on different button

I have two IBAction and want to change of the text of two buttons who refer to these IBActions.
I can change with sender. the color of the own button. But not with the other button. How can I solve this? My Code doesnt work. It only changes the own color.
#IBAction func arminiaBielefeld(_ sender: UIButton) {
team = "arminia"
Bundesliga1.bildlink = "bundesliga1/arminia"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
sender.setTitleColor(.white, for: [])
augsburg.setTitleColor(.blue, for: [])
}
#IBAction func augsburg(_ sender: UIButton) {
team = "augsburg"
Bundesliga1.bildlink = "bundesliga1/augsburg"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
sender.setTitleColor(.white, for: [])
arminiaBielefeld.setTitleColor(.blue, for: [])
}
Screenshot added.
Try using the outlet name of the button instead of sender. Also do not have the name of the action identical to the name of the outlet.
Add the following two outlets to your view controller and connect them in storyboard:
#IBOutlet weak var arminiaBielefeld: UIButton!
#IBOutlet weak var augsburg: UIButton!
The following works:
#IBAction func arminiaBielefeldPressed(_ sender: Any) {
team = "arminia"
Bundesliga1.bildlink = "bundesliga1/arminia"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
arminiaBielefeld.setTitleColor(.white, for: [])
augsburg.setTitleColor(.blue, for: [])
}
#IBAction func augsburgPressed(_ sender: Any) {
team = "augsburg"
Bundesliga1.bildlink = "bundesliga1/augsburg"
Bundesliga1.ligaIcon = "bundesliga1/bundesliga1"
augsburg.setTitleColor(.white, for: [])
arminiaBielefeld.setTitleColor(.blue, for: [])
}

Dark and light not switching with segmented control (Swift)

Well I'm trying to make a light and dark theme for a cookie clicker with segmented index but when i switch nothing happens and im really confused if anyone could help me i would really appreciate it.
Ive tried changing else if. Removing code just trying print statements in it its ad if the function
//
// ViewController.swift
// clicker
///Users/ishaanrao/Desktop/Swift/clicker/clicker/Base.lproj/Main.storyboard
// Created by Ishaan Rao on 8/1/19.
// Copyright © 2019 Ishaan Rao. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//Properties
var x: Int = 0
#IBOutlet weak var selector: UISegmentedControl!
#IBOutlet weak var resetbtn: UIButton!
#IBOutlet weak var score: UILabel!
#IBOutlet var back: UIView!
//viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
resetbtn.backgroundColor = UIColor.black
resetbtn.setTitle("Reset", for: .normal)
resetbtn.setTitleColor(.white, for: .normal)
resetbtn.layer.masksToBounds = true
resetbtn.layer.cornerRadius = 5
score.backgroundColor = UIColor.blue
score.layer.masksToBounds = true
score.layer.cornerRadius = 5
score.textColor = UIColor.white
score.textAlignment = .center
// Do any additional setup after loading the view.
}
//methods
#IBAction func cookie(_ sender: Any) {
x += 1
score.text = "Score: \(x)"
}
#IBAction func reset(_ sender: Any) {
x = 0
score.text = "Score: \(x)"
}
#IBAction func darl(_ sender: Any, forEvent event: UIEvent) {
}
#IBAction func light(_ sender: Any) {
if selector.selectedSegmentIndex==0 {
resetbtn.backgroundColor = UIColor.black
resetbtn.setTitle("Reset", for: .normal)
resetbtn.setTitleColor(.white, for: .normal)
resetbtn.layer.masksToBounds = true
resetbtn.layer.cornerRadius = 5
score.backgroundColor = UIColor.blue
score.layer.masksToBounds = true
score.layer.cornerRadius = 5
score.textColor = UIColor.white
score.textAlignment = .center
} else if selector.selectedSegmentIndex==1 {
back.backgroundColor = .black
resetbtn.backgroundColor = UIColor.white
resetbtn.setTitle("Reset", for: .normal)
resetbtn.setTitleColor(.black, for: .normal)
resetbtn.layer.masksToBounds = true
resetbtn.layer.cornerRadius = 5
score.backgroundColor = UIColor.orange
score.layer.masksToBounds = true
score.layer.cornerRadius = 5
score.textColor = UIColor.black
score.textAlignment = .center
}
}
}
I want it to change colors but its not there are no errors
It appears that the IBAction just isn't linked to the button event. Try to link the Storyboard button TouchUpInside event to the action like this:
For more info, you can follow the Oficial Apple documentation

selecting with UIButton and moving a specific model data with segue

I am new to swift and new to programming in general. I am building a quiz app. I want to select a topic in a TopicViewController and move to a new Quizviewcontroller that will display the question and answer choices. I have multiple question bank that I believe are objects of a class Question. I am able to move to the QuizViewConctroller with segue but unable to select the Question bank based on the topic UI button selected.
I have tried and spent days trying to figure this out. I have looked at similar posts in SO. I have posted this question before but did not get any reply. I would really appreciate if someone could help. I don't know how else to proceed...
TopicsViewController:
import UIKit
class TopicsViewController: UIViewController, ReturnToTopicVCDelegate {
func goToTopicVC() {}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func goToQuestionsVCWhenPressed(_ sender: UIButton) {
performSegue(withIdentifier: "segueToQuestionVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToQuestionVC" {
let quizVC = segue.destination as! QuizViewController
quizVC.delegate = self
}
}
}
QuizViewController:
import UIKit
import QuartzCore
protocol ReturnToTopicVCDelegate {
func goToTopicVC()
}
class QuizViewController: UIViewController {
var delegate: ReturnToTopicVCDelegate?
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var questionImageView: UIImageView!
#IBOutlet weak var optionAButton: UIButton!
#IBOutlet weak var optionBButton: UIButton!
#IBOutlet weak var optionCButton: UIButton!
#IBOutlet weak var optionDButton: UIButton!
#IBOutlet weak var optionEButton: UIButton!
//outlets for the progress
#IBOutlet weak var questionCounter: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
var allQuestions = QuestionBank()
var selectedAnswer: Int = 0 // answer selected by the subject
var questionNumber: Int = 0
var score: Int = 0
#IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer {
print("correct answer")
sender.backgroundColor = .green
score += 1
} else {
print("wrong")
sender.backgroundColor = .red
print("\(allQuestions.list[questionNumber].correctAnswer)")
//the following two lines change the right answer button to green using the tag value of the button
let correctAnswerButton = view.viewWithTag(selectedAnswer) as? UIButton
correctAnswerButton?.backgroundColor = UIColor.green
}
}
#IBAction func GoToNextQuestion(_ sender: UIButton) {
questionNumber += 1
nextQuestion()
}
func nextQuestion() {
if questionNumber <= allQuestions.list.count - 1 {
questionLabel.text = allQuestions.list[questionNumber].question
questionImageView.image = UIImage(named: (allQuestions.list[questionNumber].questionImage))
optionAButton.setTitle(allQuestions.list[questionNumber].optionA, for: .normal)
optionBButton.setTitle(allQuestions.list[questionNumber].optionB, for: .normal)
optionCButton.setTitle(allQuestions.list[questionNumber].optionC, for: .normal)
optionDButton.setTitle(allQuestions.list[questionNumber].optionD, for: .normal)
optionEButton.setTitle(allQuestions.list[questionNumber].optionE, for: .normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
updateUI()
} else {
let alert = UIAlertController(title: "Great!", message: "Do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default) { (UIAlertAction) in
self.restartQuiz()
}
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func updateUI() {
scoreLabel.text = "score: \(score)"
questionCounter.text = "\(questionNumber + 1)/\(allQuestions.list.count)"
}
func restartQuiz() {
score = 0
questionNumber = 0
nextQuestion()
}
#IBAction func goBackToTopicsVC(_ sender: Any) {
delegate?.goToTopicVC()
dismiss(animated: true, completion: nil)
}
}
The Questions are in this format
import Foundation
class QuestionBank {
var list = [Question]()
init() {
let skyColorQuestion = Question(questionText: “What is the color of sky?", image: "sky", choiceA: "blue", choiceB: "black", choiceC: "yellow", choiceD: "pink", choiceE: "None of the above", answer: 1)
let whatQuestion = Question(questionText: “what…?”, image: "image", choiceA: "x", choiceB: "y", choiceC: "z", choiceD: "m", choiceE: "None of the above", answer: 3)
list.append(skyColorQuestion)
list.append(whatQuestion)
}
}
Navigation Pane
Storyboard
Add the data are not correct in this way. My recommendation you can use a Realm, sqLite, CoreData or FireBase.

Moving data from Model to destination ViewController with segue

I am new to Swift and programming in general. I am building a quiz app. The app uses TopicsViewController to select a topic and segue to a QuestionsViewController. The questions for the various topics are stored as separate Swift Objects file. I would like to pick the Topic1 Question file when I press the topic1 button in TopicsViewController to segue into the QuestionsViewController. I would like to know how can I select the particular questions file QuestionBank1/QuestionBank2 when I select the particular topic upon segueing to the QuestionsViewController?
Navigation Pane :
Main.storyboard :
TopicsViewController:
import UIKit
class TopicsViewController: UIViewController, returnToTopicVCDelegate {
func goToTopicVC() {
}
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func goToQuestionsVCWhenPressed(_ sender: UIButton) {
performSegue(withIdentifier: "segueToQuestionVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToQuestionVC" {
let quizVC = segue.destination as! QuizViewController
quizVC.delegate = self
}
}
}
QuizViewController:
import UIKit
import QuartzCore
protocol returnToTopicVCDelegate{
func goToTopicVC()
}
class QuizViewController: UIViewController {
var delegate : returnToTopicVCDelegate?
//outlet for the question label and image view
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var questionImageView: UIImageView!
//outlet for the buttons
#IBOutlet weak var optionAButton: UIButton!
#IBOutlet weak var optionBButton: UIButton!
#IBOutlet weak var optionCButton: UIButton!
#IBOutlet weak var optionDButton: UIButton!
#IBOutlet weak var optionEButton: UIButton!
//outlets for the progress
#IBOutlet weak var questionCounter: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
var allQuestions = QuestionBank()
var selectedAnswer : Int = 0 // answer selected by the subject
var questionNumber: Int = 0
var score: Int = 0
// functions executed after an answer is picked
#IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer {
print("correct answer")
sender.backgroundColor = .green
score += 1
} else {
print("wrong")
sender.backgroundColor = .red
print("\(allQuestions.list[questionNumber].correctAnswer)")
//the following two lines change the right answer button to green using the tag value of the button
let correctAnswerButton = view.viewWithTag(selectedAnswer) as? UIButton
correctAnswerButton?.backgroundColor = UIColor.green
}
}
#IBAction func GoToNextQuestion(_ sender: UIButton) {
questionNumber += 1
nextQuestion()
}
// selects a new questions and updates the score
func nextQuestion(){
if questionNumber <= allQuestions.list.count - 1 {
questionLabel.text = allQuestions.list[questionNumber].question
questionImageView.image = UIImage(named: (allQuestions.list[questionNumber].questionImage))
optionAButton.setTitle(allQuestions.list[questionNumber].optionA, for: UIControlState.normal)
optionBButton.setTitle(allQuestions.list[questionNumber].optionB, for: UIControlState.normal)
optionCButton.setTitle(allQuestions.list[questionNumber].optionC, for: UIControlState.normal)
optionDButton.setTitle(allQuestions.list[questionNumber].optionD, for: UIControlState.normal)
optionEButton.setTitle(allQuestions.list[questionNumber].optionE, for: UIControlState.normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
updateUI()
} else {
let alert = UIAlertController(title: "Great!", message: "Do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default) {(UIAlertAction) in
self.restartQuiz()
}
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func updateUI(){
scoreLabel.text = "score: \(score)"
questionCounter.text = "\(questionNumber + 1)/\(allQuestions.list.count)"
}
func restartQuiz(){
score = 0
questionNumber = 0
nextQuestion()
}
#IBAction func goBackToTopicsVC(_ sender: Any) {
delegate?.goToTopicVC()
dismiss(animated: true, completion: nil)
}
}
You can use the following steps :
1- Add a segue from TopicsViewController to QuestionsViewController and give the segue "Identifier Name " from Attributes inspector.
2- Add a variable in QuestionsViewController for the topic lets name it "topicType".
3- Override the below function in TopicsViewController and send the name of the topic with the segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Identifier Name" {
if let destinationviewController = segue.destination as? QuestionsViewController , let buttonPressed = sender as? UIButton {
destinationviewController.topicType = buttonPressed.currentTitle!
}
}
}
4- For each button in TopicsViewController , get the button action and type the following function in it :
#IBAction func topicButton(_ sender: UIButton) {
performSegue(withIdentifier: "Identifier Name", sender: nil)
}
I hope this helps you .

UIButton image in Swift does not change colors even after a function is created

I am using Xcode 8 and Swift 3, and am trying to create a button that changes colors when I click on it.
the button is black, but should turn blue when I click. The button is an image of a piggy bank.
I went ahead and added an image of the black pig and blue pig, with names money_off and money_on, respectively.
I am now in the process of writing code to turn the black image blue upon click. The code itself shows no errors, but the simulation was not successful. Clicking on the black piggy bank does nothing.
Does anyone have a clue what might be happening?
import UIKit
class SecondViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var label: UILabel!
let step: Float = 1
#IBOutlet weak var moneybutton: UIButton!
#IBOutlet var itemTextField: UITextField!
#IBAction func moneyClicked(_ sender: UIButton) {
if sender.currentImage === #imageLiteral(resourceName: "money_off"){
sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
else {
sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
}
}
}
Try this-
class SecondViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var moneybutton: UIButton!
func viewDidLoad() {
self.moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
self.moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
}
#IBAction func moneyClicked(_ sender: UIButton) {
self.moneybutton.isSelected = !self.moneybutton.isSelected
}
}
You better not rely on the return value of some #imageLiteral, but instead keep a state, like moneyOff in the example code below.
(sorry, no Xcode available, so code might contain some syntax errors, but you'll get the idea):
class SecondViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var label: UILabel!
let step: Float = 1
var moneyOff = true
#IBOutlet weak var moneybutton: UIButton!
#IBOutlet var itemTextField: UITextField!
#IBAction func moneyClicked(_ sender: UIButton) {
if moneyOff {
sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
else {
sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
}
moneyOff = !moneyOff // toggle state
}
}
I guess problem is your connection between storyboard and view controller.
I try to add button programmatically and it work.
import UIKit
import AVFoundation
class ViewController: UIViewController {
lazy var btn: UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClicked)))
btn.isUserInteractionEnabled = true
btn.setImage(UIImage(named: "money_off"), for: .normal)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
view.addSubview(btn)
btn.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
btn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 50).isActive = true
btn.widthAnchor.constraint(equalToConstant: 50).isActive = true
btn.heightAnchor.constraint(equalToConstant: 10).isActive = true
}
func handleClicked() {
if btn.currentImage == UIImage(named: "money_off") {
btn.setImage(UIImage(named: "money_on"), for: .normal)
} else {
btn.setImage(UIImage(named: "money_off"), for: .normal)
}
}
}
Below code is work for me:
class ViewController: UIViewController {
#IBOutlet weak var moneybutton: UIButton!
func viewDidLoad()
{
moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
}
#IBAction func moneyClicked(_ sender: UIButton)
{
if self.btn.isSelected
{
self.moneybutton.isSelected = false
}
else{
self.moneybutton.isSelected = true
}
}