I have an array of buttons and at the beginning of the program I want the user to pick one button and then it'll change its background.
I've written a switch-statement but I don't know how can I implement it into override func viewDidLoad(), namely, I have no idea what should I write as a parameter instead of UIButton
class ViewController: UIViewController {
#IBOutlet var cardsArray: Array<UIButton> = []
override func viewDidLoad() {
super.viewDidLoad()
self.playerMove(sender: UIButton)
// 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.
}
#IBAction func playerMove(sender: UIButton) {
switch (sender) {
case self.cardsArray[0]:
self.cardPressedAll(0)
case self.cardsArray[1]:
self.cardPressedAll(1)
case self.cardsArray[2]:
self.cardPressedAll(2)
default: break
}
}
func cardPressedAll (cardNumber: Int) {
self.cardsArray[cardNumber].enabled = false
self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
}
}
Instead of calling 'self.playerMove(sender: UIButton)' in your view did load you need to connect your buttons to your IBAction using storyboard.
Related
import UIKit
class ViewController: UIViewController {
// #IBAction func Btndel(_ sender: Any) {
//}
var Str:String?
override func viewDidLoad() {
super.viewDidLoad()
let items = [Str]
let SegM = UISegmentedControl(items:items as Any as? [Any])
SegM.selectedSegmentIndex = 0
SegM.frame=CGRect(x: 70, y: 130, width: 100, height: 50)
SegM.layer.cornerRadius = 8.0
SegM.backgroundColor = .orange
SegM.tintColor = .white
self.view .addSubview(SegM)
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Btnadd(_ sender: Any)
{
var Str = 0;Str += 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How to add and delete segments in a viewcontroller by clicking add button an delete button created in the same viewcontroller
How to insert and remove(add or delete) segments in swift4
class ViewController: UIViewController {
#IBOutlet weak var segment1: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func insert(_ sender: Any) {
segment1.insertSegment(withTitle: "\(segment1.numberOfSegments+1)", at: segment1.numberOfSegments, animated: true)
}
#IBAction func remove(_ sender: Any) {
segment1.removeSegment(at: segment1.numberOfSegments-1, animated: true)
}
}
You can insert segment by insertSegment method of UISegmentedControl and you can delete segment by removeSegment method. Let me take an example.
I create segmentController class and its UI in a storyboard.
Below is UI screenshot. In the storyboard, You can see two buttons Insert (+) and Remove (-) and UISegmentedControl. Insert button will insert segment at a specific position and Remove button will remove the segment at a specific position.
Below is code of segmentController class.
class segmentController: UIViewController {
#IBOutlet weak var segementControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func remove(_ sender: Any) {
segementControl.removeSegment(at: segementControl.numberOfSegments-1, animated: true)
}
#IBAction func insert(_ sender: Any) {
segementControl.insertSegment(withTitle: "\(segementControl.numberOfSegments+1)", at: segementControl.numberOfSegments, animated: true)
}
}
In the above code, on insert button click new segment will add at the last of segementControl. On remove button click the last segment will delete from segmentControl.
Hope it helps.
I made a simple code to clarify my problem. When I run my app on my iPhone it works properly, but when I press home button - either on simulator or my real iPhone - and press the app icon again, the app runs, but the app's button "TestButtonAction don't work. Same problem when I press power/sleep button and turn it on again.
same problem either when I duple click home button and terminate my app by sliding it's window away, and rerun it again the button don't work or print "Right Image" in the Console
Here is my code, Thanks.
//
// ViewController.swift
// TestingHomeButton
//
// Created by Samuel Oncy on 4/25/18.
// Copyright © 2018 Samuel Oncy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
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.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
if(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView")){
print("Right Image")
}
}
}
Simply Make UIImage global Variable, If you want to compare in If.
Other ways can also be to solve same problem - Tag is right one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
var image = UIImage (named: "NatureView")
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.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
// print(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView"))
if (TestButtonOutlet.currentImage == image) {
print(TestButtonOutlet.currentImage)
}
}
}
To check the current image of button, use the currentImage property as follows:-
#IBAction func TestButtonAction(_ sender: UIButton) {
if sender.currentImage.isEqual(UIImage(named: "NatureView")) {
print("Right Image")
}
}
There are many answers but they don't answer the question. For example, I put the following code in:
override func viewWillDisappear(animated: Bool) {
if (self.navigationController!.viewControllers.indexOf(self)==nil) {
print("back button pressed\n")
}
super.viewWillDisappear(animated)
}
But 'back button pressed' gets printed to the console even when the back button has not been pressed. The scene has buttons that return to the previous scene using unwind segues, and these cause 'back button pressed' to be printed. I need to execute code only if the back button has been pressed.
Edit for Muneeba:
import UIKit
class NewViewController: UIViewController, UINavigationBarDelegate {
func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem) {
performSegueWithIdentifier("returnToStepOne", sender: self)
delegate.backFromNewViewController()
}
override func viewDidLoad() {
super.viewDidLoad()
// self.navigationController.navigationBar.delegate = self;
navigationController?.navigationBar.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try with following UINavigationBarDelegate.
func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)
I'm working on a simple guessing game app, just to get more comfortable with Swift and Xcode. I have been able to input within userInput and get it to print a message to the console, however when I try to get it to print my input to usersGuess(which is a label), I can not figure it out.
Here's my code within a single view application via Xcode:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
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.
}
#IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
I'm sure this is simple, but I am scratching my head lol.
#IBAction func userInput(sender: UITextField) {
println("This is working")
usersGuess.text = sender.text
}
Although I am still new to iOS dev and Swift, I think you could also take a look at the use of delegate in this tutorial Apple provides. I guess it might be the code didn't resign your text field's first-responder status. Hence, the usersGuess could not update. (Anyone who knows how this work please leave a comment.)
To do this, basically
Create an outlet for the UITextField that receives user's input, say, usersInput.
Set ViewController as a delegate of usersInput, which will
Resign the first-responder status of usersInput when the Return button on the keyboard is pressed.
Update the text of usersGuess.
Code here:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var correctAnswerLabel: UILabel!
#IBOutlet weak var usersGuess: UILabel!
#IBOutlet weak var usersInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set ViewController as a delegate
usersInput.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Here are the callback functions for usersInput
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
usersGuess.text = textField.text
}
#IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
#IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
I am Created 2 Views, One is and Used Protocol and Delegate. For first view the Delegate function is not called.
My FirstView Controller : Here I am Accessing the Delegate Function.
import UIKit
class NextViewController: UIViewController,DurationSelectDelegate {
//var secondController: DurationDel?
var secondController: DurationDel = DurationDel()
#IBAction func Next(sender : AnyObject)
{
let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
self.navigationController.pushViewController(nextViewController, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
secondController.delegate=self
}
func DurationSelected() {
println("SUCCESS")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
My SecondView Controller : Here I Am creating Delegate.
import UIKit
protocol DurationSelectDelegate {
func DurationSelected()
}
class DurationDel: UIViewController {
var delegate: DurationSelectDelegate?
#IBAction func Previous(sender : AnyObject) {
//let game = DurationSelectDelegate()
delegate?.DurationSelected()
self.navigationController.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
To me, it looks like you're pushing a view controller that you haven't actually set the delegate for. If you change your "Next" function, to include the line
nextViewController.delegate = self
You should see that the delegation works. In doing this, you can also probably remove the creation of "secondController", as it looks like that's redundant.
The naming convention you have followed would confuse fellow developers in your team. The instance should have been
let durationDel = DurationDel(nibName: "DurationDel", bundle: nil)
And then as #Eagerod mentioned, the delegate you would set is
durationDel.delegate = self