Selecting the name of a UIButton swift5 - swift

I have a UIButton called picPressed, this UIButton has a background image. I want to to be able to get the name of the buttons background image.
I am trying something like:
let hello = picPressed.backgroundImage.name()
Print(hello)
But get an error saying:
value of type '(UIControl.State) -> UIImage?' has no member 'name'
How can isolate this attribute in swift5?

No, buttons don't remember their names (or the name of the image you install into them.)
I suggest you create an object that manages a button, takes a filename for an image, and remembers that filename for later. (Alternately you could subclass UIButton, but that can get complex.)

Related

Trying to add two images in a UI image

Im new to xcode and was trying to add different images in a table row in a storyboard layout.
let image:UIImage = UIImage(named:
”ster.jpg")!
when i use this code, it makes every table the same image
let image:UIImage = UIImage(named:
”ster.jpg", "blue.jpg")!
My goal is for the 2nd row of the table to have a different image, so when using this code it just creates an error saying " Extra argument in call"
I would start by saying that the way you initialize the UIImage object is wrong. It supports only one image at a time. Thus, you get the error of an extra argument in the call.
To add two images in a row, you better use a horizontal stack view and modify some of its properties (fill equally, spacing, etc).
The way I would do the thing: Drag and drop from library 2 UIImage objects. Embed them in a horizontal stack view. In property inspector set the fill property to equal. Make sure to add constraints.
In case you are using UITableView and want to modify the cell, you will have to create a subclass of UITableViewCell and give it an identifier to use when the TableViewDataSourceDelegate object is fetching the cell data based on the identifier.

Edit Name of cell upon tap - Swift

I want to edit the text in a cell by tapping and I was wondering if there is a way to do it other than by adding a text field. is there something in the table view delegate that I missed?
You can create cell (or any other UIResponder subclass) that conforms UITextInput protocol (or maybe UIKeyInput is enough). Here is simple example (old link)
Notice,
you should set cell.canBecomeFirstResponder = true.
To show keyboard use cell.becomeFirstResponder()
Maybe there something else, that you should do. I recommend reading docs.
...Or use UITextField :)
We use an array to display no of text label in cell.
so you can do is, you can use a text field and a button on a view.
In button action add a line of code like stringArray[2] = "new string" to replace the particular element from array and when you enter a text and press a button the element will get replaced and just reload the table view.
And you can also write the method in didSelectRowAtIndexPath, write stringArray[indexPath.row] = "new string" and reload the table view and it is done.

Why cant I display the layer property value of Uibutton

I was just experimenting with layer property of my UIButton by setting a outlet n the code for a button created in xib.
NSLog(#"d button layer value%#",dButton.layer.backgroundColor);
But the output is coming as:
d button layer value(null)
My question is: can't we display the layer property value of UIButton. We all know that some default value would have been set for the button.
According to the documentation for CALayer:
The default is nil.
unless you explicitly change it.
In the documentation for UIView, it says this:
The default value is nil, which results in a transparent background color.
This excerpt describes the view.backgroundColor property, so I assume that the view.layer.backgroundColor property is the same.
Hope this helps!
Phew !! I finally got the answer.
Actually I was trying to display the structure value using %# in NSlog statement.
dButton.layer.backgroundColor is a fundamental data type used internally by Quartz to represent colors.
So, way to display them is to make your own class which can parse the value.
Like, for example there are class functions for CGRect, CGPoint etc
like for displaying CGRect we use this code
NSLog(#"d button layer value%#",NSStringFromCGRect(dButton.layer.bounds));
but there arent any function defined for CGColorRef.
Ok, as per jrturton said,for displaying the above value we can use
NSLog(#"d button layer value%#",[UIColor colorWithCGColor:button.layer.backgroundColor)]
I hope everyone got it now !!

How to get current value of UITextField in cocos2d 0.8.2?

Hi all I am using cocos2d 0.8.2 for my new game. I have added a UITextField in my game and on click of a button (this button is actually a sprite i m using ccTouchesEnded! ) i save text field's value in database, but whenever i access the value i am getting nil. if i initialize the textfield value by some string, it always give me only that value.
i.e.
[txtField setText:#"Enter Your Name"];
when i access the value of txtField on a click of button it always give me "Enter Your Name". Although the value is changing when i type in textField, but its not returning me the new value.
is this a issue with cocos2d 0.8.2 or i am missing something?
You don't save the entered text. You just set #"Enter your Name" as default text, that is displayed in the cell. You should have a look at the UITextFieldDelegate methods. You have to overwrite the method didEndEditing, that is called after your editing or typing in text has ended, and implement there a line that saves the current value. Here you can use something like this:
NSString *containerForTextFieldValue = textField.text;

Assigning different labels dynamically

After detecting Button sender through its tag in an action method, I want to dynamically assign some text to the corresponding Label (say, button tag+10 ) using some sort of temp UILabel var.
Hoping that's understandable?..english is not my native language :)
thank you.
In the containing view, you can use viewWithTag to get the object with a specific tag.
If you know the object is a UILabel, you can typecast it to get your label object.
If you are in your viewcontroller.m file:
UILabel* myLabel;
myLabel = (UILabel*)[self.view viewWithTag:(buttonTag+10)];
Good luck!