(swift) Anyone can help me to make bulk change to those buttons? - swift

here is the screenshot
when user get into this page, I want to change the background of those button.
for example, when the level of user is 6, I want to change the top 6 background of buttons to a blue picture as first button. I don't wanna write a code one by one, so please help me to write a function, Thank you very much!

You can add you buttons to the array in order of increasing level and do something like this:
var buttons: [UIButton] // array of your buttons, next you need to init it
...
func setColorForUserLevel(userLevel: Int) {
if userLevel > buttons.count {
return
} else {
for i in 0...userLevel - 1 {
buttons[i].backgroundColor = UIColor.blueColor()
}
}
}
I create simple example project to show how to use it:
I have outlets with UIButton! type and buttons object in code:
And i test setColorForUserLevel function in viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
buttons = [button1, button2, button3]
setColorForUserLevel(2)
}

Related

Cannot display UIActivityIndicatorView

I meet a question. I am using following code to display UIActivityIndicatorView. My requirement is to be able to create an UIActivityIndicatorView and display it when I click the button with tag 1, if I click other buttons the UIActivityIndicatorView will be removed from the super view.
class ViewController: UIViewController {
lazy var acLoad:(UIActivityIndicatorView) = {
let myActivityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.white)
myActivityIndicator.center = view.center
myActivityIndicator.hidesWhenStopped = true
myActivityIndicator.startAnimating()
return myActivityIndicator
}()
//more code
#objc func btnAction(sender: UIButton){
switch sender.tag {
case 1:
print("created")
view.addSubview(acLoad)
acLoad.startAnimating()
default:
print("removed")
acLoad.stopAnimating()
acLoad.removeFromSuperview()
}
}
}
The above doesn't work, I can get the print log after click, but UIActivityIndicatorView doesn't display, any ideas?
It seems like your UIActivityIndicatorView might be misplaced (wrong frame value) - so it is not visible to you.
print("created")
view.addSubview(acLoad)
acLoad.startAnimating()
// Add this line in your button tap code
// It will tell you where it is placed inside your view
print(acLoad.frame)
If you find that it's frame is not where you want it to be, just fix your layout code and it will be where you expect it to be.
Another note - myActivityIndicator.hidesWhenStopped = true makes it automatically hide on stopAnimating() call. So you can add it only once, not remove-add every time.
Also check your view.backgroundColor vs acLoad style.

I can't change the text of a UILabel in Swift

I am trying to change the text of a UILabel in my code, but the text won't change.
I tried to use the well-known command for changing the text, "NameOfLabel.text = 'Hello", but that did not work. So I tried to put it in a start function so you would click a UIButton and it would change the text, didn't work either.
#IBOutlet var nameOfRobot: UILabel!
#IBAction func startButton(_ sender: Any){
let nameNumber = Int.random(in: 1...3)
if nameNumber == 1 {
self.nameOfRobot.text = "Ben"
}
if nameNumber == 2 {
self.nameOfRobot.text = "Oliver"
}
if nameNumber == 3 {
self.nameOfRobot.text = "Colton"
}
}
I want it to choose a number between 1 and three and have it change the UILabel to that name. When I start the app though, it works, but it doesn't change the text of the label.
Looks like you forgot to connect the action to the button press:
You can tell this is done correctly by looking at the full circle indicator in the editor:
EDIT: Setting the correct class to the viewController in the storyboard:

Swift save text to an instance/global variable when UIButton is pressed

So I have a UItextfield and a UIButton I was wondering if there is a way to save the text after the button is pressed in an instance variable so that I could use it in my other class.
The thing I am trying to do is to have a user input some text in the textfield on screen 1 and then when the user taps the button I want to take him to screen 2 however in the class of that screen 2 I want to know what text user entered in order to display data accordingly.
Right now I just have an action function for the button and I access text inside it and save it to an instance variable but when I call it in my other class it is empty because I initialized it as empty. So can somebody please help me and tell me how to go about this.
Thanks!
Here is the code for first screen
var searchRecipe = ""
#IBAction func SearchButton(sender: UIButton) {
if let recipe = RecipeSearchBar.text {
searchRecipe = recipe
}
}
Here is the code for second screen. I have connected the button in first screen this screen so when user taps the button he gets here.
var recipeName:[String] = []
var imageURL:[String] = []
var timeInSeconds:[Float] = []
func apiCall()
{
//search recipe API call
var searchRecipe = ""
searchRecipe = RecipesViewController().searchRecipe
print (searchRecipe) //prints nothing
endpoint = "http://api.yummly.com/v1/api/recipes?_app_id=apiId&_app_key=apiKey&q=\(searchRecipe)" //this is where I want to use the searchRecipe text from class 1
}
Well personally I would do what follows (Based on my interpretation of your issue):
Please note that I take for granted you are implementing a view-controller-based navigation and you know what IBOutlets, IBActions & Segues are
In the first view controller create an IBOutlet property of
UITextField named RecipeSearchBar and connect it to the relative text
field.
In the second view controller create a variable value of type
string;
Change the storyboard segue identifier to "toSecondViewController"
Then in the first view controller create an IBAction to be called
when the UIButton is pressed.
In your case:
#IBAction func SearchButton(sender: UIButton) {
if let recipe = RecipeSearchBar.text {
searchRecipe = recipe
}
//If you are navigating into a new view view controller
//here you need to call self.presentViewController(...)
}
Inside this action, you are going to call self.presentViewController
to display your second view controller, but you have to do one last
very important step: pass the value of the UITextField to the instance
that will hold the second view controller
To achieve this, override prepareForSegue in your first view controller
and share the UITextField value. This method is triggered once you called self.presentViewController for further implementation.
//Did not tested the code but should just be ok
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toSecondViewController")
{
//Create the instance
let secondViewController = segue.destinationViewController
//Set the value
secondViewController.value = searchRecipe;
}
}
You are now good to go.
Hope this helped, if so mark the question for others.
Bye

Hide button function if statement issue - Swift

I have a function to hide a slider if another button is pressed. I currently have:
#IBOutlet weak var sliderHide: UISlider!
#IBAction func sliderHide(sender: UISlider) {
if (buttonPlay.selected) {
sliderHide.hidden = false
}
else if (!buttonPlay.selected) {
sliderHide.hidden = true
}
}
The build is running but the slider only hides if itself is selected. It does nothing if the button is selected.
Use == not = when comparing,= is an assignment operator.
Moreover, if buttonImage is not a variable representing the control state of a button, but the button itself you want to do:
if buttonImage.selected == false {
}
However, you should use an #IBAction for the button if you want to run a function when it is pressed.
Please consider using an #IBAction for this kind of thing unless your button is programmatically created. You can read more about it here:
http://rshankar.com/different-ways-to-connect-ibaction-to-uibutton/
I
I think you want
add an Outlet to the slider like the picture and then wherever you play the sound, make the sliderYouMightHide.hidden = false and wherever the sound stops make the sliderYouMightHide.hidden = true. You might also want to make it hidden in viewDidLoad

Hide add button

I am trying to have a button, so the user can hide the add banner if they want to, this works but when the add banner is not displaying I want the button to also be hidden. So I added the following code, but as soon as I did so, the button stopped working. Any ideas as to why?
thank you!
override func viewDidLoad() {
super.viewDidLoad()
if Banner.hidden == false{
hideAdd.hidden = false
} else {
hideAdd.hidden = true
}
The following is the code I used to hide the banner
#IBAction func HideAddButton(sender: AnyObject) {
Banner.hidden = true
}
The easiest way to hide and unhide objects on a MainStoryboard is to change the alpha to 0 in the attributes inspector. Then when you want it to appear you can just change the alpha in your code to 1 when you want it to appear in ViewController.swift. Or Vice Versa.