How can I tell which button was pressed LAST - SWIFT? - swift

Pretty basic - I have multiple buttons and I just need to tell which button was pressed most recently, despite how many times on each or how long between clicks.

Give each button a unique tag in Interface Builder.
Make a variable that holds the tag of the last button that has been clicked.
In event handlers of your buttons set the variable from the step above to the tag of the button currently being clicked.
When you need to find the last button that has been clicked, check the variable, it will have the unique tag of your button.

Related

Display a message when cursor over on UILabel

How can I display some text message when a label is tapped?
I have a label (Address).Now and when I tap over on it, I need to display the corresponding address in a small popup.
How can I do it?
I found out from your comment that you actually want to have a pop up on tap event.
All you need to do is
Have a button on the UILabel (on which you want to show pop up).
Make button of cutom type (It will make the button disappeared).
After that write a method to show pop up, and connect it to that custom button.
For creating a pop up, you can use UIView of desired size and position & make it appear/disappear on custom button tap event (touch up inside).
Hope this helps.

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

Adding a button to last UITableView row

I'm developing an app where the user can choose between a number of included songs. I also want the user to be able to choose a song from his/her iPod Library.
Currently the song is choosen by selecting it in a UITableView. So I figure I would like to add a new row at the end of the table and make it a button that will fire a MPMediaPickerController. All songs are placed in an array consisting of their names.
My question is how I add this last row? And also how I can "save" the selected song (or the path to it) to be used in the parent viewcontroller?
Well, you can use the UITableViewCell directly as a button itself, so when a user clicks on the last row, the action is being executed. But if I understand you right, you want to add a specific extra button as a subview of a UITableViewCell. That means if the cell (the last row) is being built, you compose your button, add its target and action, and add the button just as simple subview of the cell.
Well, the parent should now receive the message that the button has been pushed. I would do this using NSNotification, that is very easy to use, just check out the Apple documentation and take a look at an example. You can even send the selected song or its name path via the notification directly to the parent controller, where you can handle this notification.

Calculator application, pupulating textfield from buttons, iphone

I am creating a simple calculator application. I have 10 buttons labelled 0 through 9, and 5 buttons for the operations. I have an uitextfied which should be populated from the buttons. ie when the user click button 1, 1 should be entered in the field and so on. Also the value entered should be there even if the user taps another one. how to do that..pls help..
You need to declare the textfield (and the buttons) as Outlets in Interface Builder. Then you need to connect the touch up inside event of each button to a method. You can either have 10 methods which is one for each button or just one method and filter on id or tag. In the button actions you can easily update the textfield by appending the the correct number.
I would personally store user input in a NSMutableString, validating input (for numeracy)and adding it to the variable if validation returns true.
Then when a button is pressed it is simply a matter of updating the UITextField with the contents of the NSMutableString.
For the purpose of calculations, the string value can be converted to the appropriate numeric type when a calculation is required.

UIPickerView - Selects row too fast

I am currently using a UIPIckerView in my app to allow a user to select from a list of options. The problem is that there isn't enough of a delay when the user stops spinning the wheel and it is selecting a value before the user has a chance to scroll further down the list.
Is there a way to override the default behavior that selects the row as soon as the wheel stops spinning and the user removes their finger? I see Mobile Safari includes a "Done" button which would be great.
I can provide code if necessary (not sure how it would help).
Thanks!
You can add this manually; just add a done button to the view that holds the UIPicker, and have IT do whatever action you're currently performing in – pickerView:didSelectRow:inComponent:.
The UIPickerView automatically selects which ever row stops in the center. It does not work like a table but more like a popup menu. As such, you can't use a picker view like a button to call an action because it will trigger the moment the user stops moving it whether that represents their final choice or not.
Instead, as noted previously, you need a second control element (usually a button) to call the action that makes use of the pickerview's selection.