How to focus on an alternate label using cocoa? - cocoa-touch

I'm still a noob to iPhone development so sorry for the dumb question. I'm creating an app that has a custom numeric keyboard. I have an IBAction 'buttonDigitPressed' that when a digit is pressed it will simply add the digit to a UILabel. Now I have another label that I wish to do the same yet I'm not sure how to gain 'focus' of that particular label. I placed an invisible button over the second label so when pressed hopefully I can call an action to switch the keyboard from the first label to the second label. Yet I have no clue how to accomplish this. Any help would be greatly appreciated!

First of all, having to put an invisible button over the UILabel is skanky. If you want the user to be able to tap a number to mean "next time I hit a digit, append its value to this number", you'll probably be happier if you just use a UIButton right from the start. You can make a UIButton that looks pretty much like a UILabel (i.e. it has no border or background color). To set a UIButton's title in code, call setTitle:forState: with the UIControlStateNormal state. So now you've got tappable numbers, i.e. these UIButtons.
Okay, so now let's pretend you've got, say, three UIButtons. You need to use an instance variable here. So, each button will have an action - let's say doButton:(id)sender. So doButton will store the value of sender (the button that was just pressed) in the instance variable. Now your buttonDigitPressed action just looks in that instance variable to learn which button to append to. That's part of the power of instance variables - they give a method in a class a place to store a value where another method in the same class can get at it.

Related

UITextField losing focus after button press and text field switch (keyboard remains visible)

I've got a UITableViewCell subclass here that manages a UITextField setup on the right side of the table view cell. Anywhere from 4 to 8 of these cells are displayed at any given time depending on the table; I use them for unit data entry (ie, entering in distances, temperatures, etc) so there's quite a bit of logic bolted to the cell subclass.
For whatever reason, I've noticed that if I perform the following steps:
1) Tap on a text field to begin editing and bring up the keyboard
2) Enter in some text
3) Tap the clear button (which is enabled on the text field)
4) Tap on another textfield in the same table view
Then the current UITextField loses focus, but the second text field does not gain it. This means that no UITextField currently has focus, but the keyboard is still being displayed on-screen... but without any active text field, it does nothing, and cannot be dismissed (presumably because there's no first responder to resign?).
I can then tap on another text field again, and it will take focus and begin editing- at which point the keyboard becomes operable again and pressing the return/done key will dismiss it and end editing as usual.
If I simply tap on another UITextField without first hitting a button, then the second UITextField will gain focus immediately (as I'd expect it to). But it seems like button presses outside of the UITextField will cause this behaviour to occur if you try to switch fields after tapping any kind of button other than the text field.
Does anyone know what is causing this? It almost sounds like there's something wrong with the responder chain, but I'm not sure what the problem would be or how to fix it.
Firstly,you are sure the textfiled in the table has a unique identifier ,such as tag.
Secondly,you should make another textfiled become first responder if you want a textfiled lost first responder but the keyboard still appear.
Figured out what it was...
The problem was that I was reloading the table data in the delegate method that my custom cell was calling upon edit completion. Apparently reloading the tableview data while you're in the middle of switching UITextFields will cause the second text field to not gain focus (but the keyboard won't get dismissed), hence causing the issue I was seeing.

How to add a second UILabel to a UIButton, configurable per UIControlState

I want to display 2 strings (at different positions in the button) with different fonts and colors (I'm using the button's setTitle for one, and I need another one), and some attributes must be changed based on the current UIControlState (like the color).
So, I'm searching the best way to add a second label to a UIButton.
I need the label to be configurable per UIControlState (I want a different color for UIControlStateNormal and UIControleStateHighlighted for example).
I've tried the following approches:
Subclass a UIButton and use drawRect: while not recommended (and I now understand why), I don't think it's even possible, it looks like the button's drawRect method is called (and after the one of my subclass) even if I don't call super.
Create a new UILabel and add it as a subview to my button: this is working quite well, except I don't know how to change the color when the UIControlState of the button is changing
Create a new layer and use drawLayer: I don't know how to get the drawLayer method to be called every time the button state is changing (my drawLayer only gets called once, when I use setNeedsDisplay just after adding my layer to the button)
Is there another way to achieve what I'm trying to do, or maybe one of those solutions might work (with a few tweaks)?
Thanks!
The second of your approach works fine. Just add 2 targets: First update to "normal state" target using "all touch events". Second update to "highlighted" using "touch down" event.
If the states are not only changed by touches and want to handle this more generally, Id suggest multithreading. All you really need is calling performSelectorInBackground when initializing all this elements (the selector updates label according to button state) and then again call same performSelectorInBackground on the end of "update label" method, creating an infinite loop.
Ok, I think I found a working solution (for my problem at least).
I'm subclassing the UIButton class (it works for me, since I'm using a custom drawn button anyway), and I override the titleRectForContentRect method that gets called everytime the title has to be displayed (including after a state change, just before display).
I added an UILabel to the button's view to display the second string I want, and during the titleRectForContentRect, I compute the correct frame location for my label, I update my label's text font and color based on the button's state (self.state), and that's all I need.

Getting UIPicker to appear when user selects a UITextField

I have a simple UITextField called month where I get users to simply enter the month they want via the keyboard that comes up. I would now like it for them to be able to use a UIPickerDate (or UIPicker) to make this selection instead. So when they press on the text field, a mini UIPicker appears and they make there selection, press anywhere on the screen and the picker disappears.
Does anyone know how to do this or has any suggestions? I am pretty new to programming and have looked at other answers but everyone seems to be referring to this being done in a table.
Thanks in advance!
You can set the inputView property on the UITextField to be an instance of UIDatePicker. When the instance of UITextField becomes the first responder, the picker view will be displayed with the standard keyboard animation.
// Assume that self.monthTextField and self.datePicker
// are properties of the view controller class
self.monthTextField.inputView = self.datePicker;
As for dismissing, that depends on the context. If there are more text fields to populate, consider adding a UIToolbar as the inputAccessoryView of self.monthTextField. Then you can add something like a UIBarButtonItem to make the next text field the first responder, similar to how the standard keyboard provides a Next button.

Text change on UIButton doesn't stick

I have an UIButton in my View that says "STOP". When pressed, it should (stop the playback, of course, and) change its label to "RTN TO ZERO". This is straightforward:
stopButton.titleLabel.text = #"RTN TO ZERO";
However, the change appears only for a split second. It doesn't stick. I assume that the button (which gets highlighted when pressed) accepts and displays the new label, but somehow the highlight is reversed only later, restoring the button to the look it had before it was pressed, not honoring the label text change.
The button is conceived in IB, not programmatically.
I feel stupid. Can someone please point me in the right direction?
In the button handler, try this:
[stopButton setTitle:#"RTN TO ZERO" forState:UIControlStateNormal];
Instead of directly changing text property of titleLabel use setTitle:forState: method to set the title in different states. Please check the manual for the details of available states.
Swift version
myButton.setTitle("button text", for: UIControl.State.normal)
Use setAttributedTitle:for for attributed text. See here for how to make attributed strings in Swift.

UILabel inside a UIToolbar using IB is invisible on run, how fix?

I want to show a total inside a toolbar. I put in IB the UILabel on top of the toolbar .
However, when I run the app, the UILabel is totally invisible (but can set values on code fine).
The most bizarre thing is that in other form, all work fine. I don't see why in one form work but not in another...
Any idea in how fix this? Or why is this behaviour happening?
Don't use a UILabel.
Use a UIBarButtonItem. Then set it to style: plain. It looks like a label, but it actually borderless button. This is the general practice of displaying text on a bar.
You can also create UIBarButtonItem with a custom view in code. You are simple "wrapping" the UILabel in a UIBarButtonItem allowing you to add anything you want to a tool bar.
To add in response to comment:
Either way, you make the button "inactive" and it doesn't respond to touches. Even though it is a button, it doesn't appear to be one. This is how Apple expects to add views to a toolbar/navbar as apposed to "float things on top of it". It violates no HIG guidelines, much the opposite, it is a reccomended design technique.
To stop the glow:
Create the button programmatically, make sure it is disabled, add it to the bar, it should then be disabled, but not dim.
In IB, have you tried to select the label and use the "Bring to Font" menu item (under Layout)? It seems like you are trying to do something pretty standard.
When you try to set values, is the label coming up as nil or at address 0x0? It's possible that the label is there, but its text cannot be set because its instance is faulty (not properly connected in IB to the IBOutlet).... Just put a breakpoint on the line where you are trying to set the value(s) for the label, and verify that the label variable is not nil (or 0x0). If it's not, try setting the text and verify on the next line that its text was set properly.
drag a UIButton into your UIToolBar. Then uncheck User Interaction Enables for this button.
Customize your UIButton so that it will look like a UILabel. Hope this will help you.