Applying image to random object - xcode

I've created an NSArray with buttons and now I wish to apply an image to a randomly chosen button from that array;
let buttons:NSArray = [button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12]
let range: UInt32 = UInt32(buttons.count)
let randomNumber = Int(arc4random_uniform(range))
let buttonstring = buttons.objectAtIndex(randomNumber)
buttonstring.image() = UIImage(named: "code2")
This last piece of code is where I wish to apply the image to the randomly chosen button. But it has an error saying;
Cannot assign to value: function call returns immutable value

If buttonstring is a UIButton, you have to use something like this:
buttonstring.setImage(UIImage(named: "code2"), forState: .Normal)
Hope this helps.

Related

SwiftUI UI Test How To Display Dynamic Buttons Text Values in an app.scrollViews?

How To Display Dynamic Buttons Text Values in an app.scrollViews?
I would like to able to tap the button inside first row in the scrollViews, but not sure what the index of the button is. I tried the 1, 2 and 3 with no luck.
let scrollViewsQuery = app/*#START_MENU_TOKEN#*/.scrollViews/*[[".otherElements[\"Tabbar\"].scrollViews",".scrollViews"],[[[-1,1],[-1,0]]],[0]]#END_MENU_TOKEN#*/
let elementsQuery = scrollViewsQuery.otherElements
elementsQuery.buttons.element(boundBy: 0).tap() //
print("----------------------------------------------")
var i = 0
for element in elementsQuery.buttons.allElementsBoundByIndex{
i += 1
print(i)
print(element) //How To Display the Button Text here?
// print( elementsQuery.buttons.element(boundBy: i))
}
Assuming you only have one scrollView present, the code to tap the first button in it would be the following:
let myScrollView = app.scrollViews.firstMatch
let myScrollViewsButtons = myScrollView.buttons
let myScrollViewsFirstButton = myScrollViewButtons.firstMatch
myScrollViewsFirstButton.tap()
A button in this context is an XCUIElement, not something that is particularly printable. Buttons do have label attributes that are generally the text displayed on them...

How to make `NSPopUpButton` with `NSControl.ControlSize.mini` programmatically?

I tried setting controlSize, and it didn't work.
I tried setting NSMenuItem.attributedTitle, and it didn't work.
Setting NSMenuItem.attributedTitle made text smaller, but didn't make checkmarks smaller. Therefore result looks broken.
Here's code to reproduce.
let testFont1 = NSFont.menuFont(ofSize: NSFont.systemFontSize(for: .mini))
let menu1 = NSMenu()
menu1.font = testFont1
menu1.addItem(withTitle: "AAA", action: nil, keyEquivalent: "")
let item2 = NSMenuItem()
item2.attributedTitle = NSAttributedString(string: "BBB", attributes: [.font: testFont1])
menu1.addItem(item2)
let popup1 = NSPopUpButton()
popup1.menu = menu1
popup1.controlSize = .mini
popup1.sizeToFit()
window.contentView?.addSubview(popup1)
How to make a mini-sized pop-up button properly with no NIB and only code?
Just assign mini-size font to the NSPopUpButton instance.
popup1.font = NSFont.systemFont(ofSize: NSFont.systemFontSize(for: .mini))
Nothing else is really required. The code can be shorten like this.
let popup1 = NSPopUpButton()
popup1.controlSize = .mini
popup1.font = NSFont.systemFont(ofSize: NSFont.systemFontSize(for: .mini))
popup1.addItem(withTitle: "AAA")
popup1.addItem(withTitle: "BBB")
popup1.sizeToFit()
window.contentView?.addSubview(popup1)
This applies equally to any other control-based classes. You need to set both controlSize and font. It seems controlSize controls only graphical appearance part and font controls text renderings.

How to set default value in Eureka-Form

I am creating a series of ImageCheckRows as a SelectableSection, and I would like to set some default values. Essentially each row returns a true/false value, so somewhere there should be a simply method of setting true or false on each row. I tried the row.value but this requires a 'String?'
currentSection! <<< ImageCheckRow<String> { row in
row.tag = "\(tagname)_\(optionKey)"
row.title = optionValue as? String
row.selectableValue = optionKey as? String
if let dvkey = optionKey as? String {
print("dvkey = \(dvkey)")
if let _ = defaultValues?.value(forKey: dvkey) {
print("we found dvkey in the defaultValues dict - try row.select")
row.value = true
}
}
}.cellSetup { cell, _ in
cell.trueImage = UIImage(named: imageChecked)!
cell.falseImage = UIImage(named: imageUnchecked)!
}
I also tried using the function:
row.select()
But this didn't work either.
Then I tried moving it to the cellSetup, but this didn't work either.
Can anyone help me here?
Worked it out myself...
row.value = optionKeyValue as? String
This is a radio button style control, and as I create the row with the default radio button I set the value to the key, however it must be a string representation, even if it is numeric. Who would have guessed!
My first thought was that the radio button control is checked/unchecked, hence it should really be a true/false value.
My second thought was that the radio button control default value would be set against the group, which is a more logical place to set or change the value.
Wrong on both counts.

Photo changing every day(Xcode, Swift)

I haven't write code for this But I want some help that can it be possible to code so that it shows new photo everyday. for example I have 10 images and I want to display each image for like September 16,17,18, and so on ?
Here you go...
Call method getImageForToday() where ever you want to fetch image for today (This solution will work specific to your case, i.e. as you mentioned you have 10 images)
func getImageForToday() -> UIImage
{
let arrImages = ["image1.png", "image2.png" ... "image10.png"]
var imageName = arrImages[getLastCharacterFromTodayDate()]
return UIImage(named: imageName)
}
func getLastCharacterFromTodayDate() -> Int
{
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let component = calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: NSDate())
return component.day % 10
}
You can use the object NSDate to know what day is it and after that doing something.
First you have your dictionnary of image like that :
let images = [ UIImage(named: "image1.png")!, UIImage(named: "image2.png")!, UIImage(named: "image3.png")]
so now, you must know what day is it for the first app openning in exemple.
So you can do that :
let date = NSDate()
You put the element date in UserDefaults. (in order to save it)
You can now display a image. You make a random number and you take UIImage at this random position.
The day after, or the next app opening you recheck the NSDate with the NSDate saved and if it's different you take a other photo.

Creating a random image generator with Swift

I'm trying to make a random image appear on the screen, but I'm new to Swift and am not sure how to do this. I have three images which I want have randomly shown in the image view, when the app is opened.
How do I do this?
Generate a ramdom number from 0 to 2 and show the image by randomly generated number.
var random = arc4random_uniform(3) //returns 0 to 2 randomly
switch random {
case 0:
//show first image
case 1:
//show second image
default:
//show third image
}
If the images are named basically the same thing. For example, "Image1.png, Image2.png, and Image3.png, then you can use this code:
override func viewDidLoad() {
super.viewDidLoad()
ImageView.image = UIImage(named: "Image\(arc4random_uniform(3) + 1).png")
}
imageArr = ["1.jpeg","2.jpeg","3.jpeg","4.jpeg"]
let RandomNumber = Int(arc4random_uniform(UInt32(self.imageArr.count)))
//imageArr is array of images
let image = UIImage.init(named: "\(imageArr[RandomNumber])")
let imageView = UIImageView.init(image: image)
It works for me (Swift 4.2):
let images: [UIImage] = [ #imageLiteral(resourceName: "randomImage1"),
#imageLiteral(resourceName: "randomImage2"),
#imageLiteral(resourceName: "randomImage3")]
let randomImage = images.shuffled().randomElement()

Resources