Unable to Generate UITextField dynamically in place of UILabel? - iphone

I am trying to generate UITextField dynamically in place of UILabel.
Now i want to update that data.
I am displaying data in the UILabel from the database and there is an UIButton for editing.
When i click on that UIButton UITextField should be generated in place of UILabel and also data should be displayed in UITextField.

What you can do is to design a view with all textfield which works in two modes, first readonly (by setting userInteraction to false ) and second editing mode. This way you can avoid the use of labels. This will need only one edit button for all of the fields. if you still want to stick with your approach, you can hide the labels, use their frames to create textfields at their place and make them visible as long as you are working in edit mode. Don't forget to use
[self.view bringSubviewToFront:TEXT_FIELD];
While you add them to your view.
e
Managing the editing with the approach I mentioned earlier is mor easy and require less efforts. Hope it helps

you need to implement this textField in .h file, to get access to it when you finish aditing. Then, in your button callback:
textField = [[UITextField alloc] initWithFrame:[yourUILabel frame]];
[textField setText:yourUILabel.text];
[self.view addSubView:textField];
then, to replace it back:
[yourUILabel setText:textField.text];
[textField removeFromSuperView];

You can use the methods that others have described here but it sounds like you just want a UITextField that looks like a label and you want to control whether or not it's editable.
set enabled to YES / NO depending on whether you want the user to edit the UITextField
set the borderStyle of a UITextField (usually between UITextBorderStyleRoundedRect and UITextBorderStyleNone)
You can then toggle the enabled and borderStyle values as follows:
- on initial view: enabled: NO, border style: UITextBorderStyleNone
- on button tap: enabled: YES, border style: UITextBorderStyleRoundedRect
You don't have to mess with the view hierarchy and worry about frames, etc, this way.

Related

UIButton and UILabel unable to be removed from view

I have two UIButtons that I added to my view
[self.view addSubview:button1];
[self.view addSubview:button2];
These buttons have a selector and in the selector is a menu where the user can choose an option and this option can vary in string size for the button so I decided to remove the buttons from the view and reload them again with different string size and button size. I have am getting the information from an API call so there is where I set the buttons up to my view. I tried to do this:
[button1 removeFromSuperview];
Also tried:
[self.button1 removeFromSuperView];
Now, for the UILabel I have it inside a table view cell because the string is long and covers my detailTextLabel. I am using UITableViewCellStyleValue1 for the cells. I have tried to use NSLineBreakByWordWrapping and set the numberOfLines to 0 as well as 5 so I then decided to add the UILabel inside the table view cell so I can control how far the string goes and I can also wrap that around. Since the cell was writing over and over the label every time the cell with the table view was hidden, I decided to create the label inside the if(cell == nil) statement.Like the button, this label also gets refreshed when the buttons are pressed and a menu option is chosen so I have to remove it from the view, the same way i did the buttons. For some reason it isn't working. Anyone have any thoughts/ideas/suggestions?
I also NSLog(#"%#",[button1 superview]) after I removed it to make sure that the button was indeed (null) as well as the label, and they do show up on the terminal as null but the buttons overlap each other, in fact you can still click the old button and you can see it underneath the new button. Same goes with the label. If you need any code let me know, this problem is frustrating me so much!
Also, I am on iOS7 and for some reason my device isn't displaying the status bar. I've tried to change it inside the info.plist -> status bar style. I've tried the 3 options it has but none of them seem to work. I checked all my xib files and checked for any hidden keywords in my .m files. Thanks in advance.
Since you mentioned the button was making an API call I am guessing you may be threading that section of code. If that's the case then the reason its not working is likely because you are not allowed to update the UI in a background thread.
Try replacing:
[button1 removeFromSuperview];
with this:
[button1 performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
I deeply suggest using storyboards, they make working with the UI a lot easier! They also allow you to play with constraints to see how your UI elements react to longer text or to the screen rotating, etc...
That apple tutorial:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/SecondiOSAppTutorial/Introduction/Introduction.html
goes through a simple app with storyboards!

More button in UILabel like in AppStore any app description

I have a description of myObject and i show it in UILabel. I want to add 'More' button to my UILabel if a description is too long. On Github i have found TTTAttributedLabel which allows to use hyperlinks. The question is, is there some special features in UILabel or UITextView to resolve my issue or i have to use TTTAttributedLabel?
The best option is certainly to use TTTAttributedLabel.
UILabel and UITextView are designed simply for showing static text. There is no method for adding tappable elements.
As an alternative, before I leaned of TTTAttributedLabel I simply placed a UIButton with a custom style over my UILabel. The button was invisible but still responded to taps. This works best for static text though, as the button needs to be placed correctly on the interface to cover the correct part of the text.
With TTTAtributtedLabel is pretty easy to add a "MORE" text at the end.
You have truncationTokenString and truncationTokenStringAttributes. Super easy!
Example:
[label setTruncationTokenString:#"... MORE"];
https://github.com/mattt/TTTAttributedLabel
Documentation: http://cocoadocs.org/docsets/TTTAttributedLabel/1.8.0/Classes/TTTAttributedLabel.html#//api/name/truncationTokenString
In my case I don't need anything more because I just change the numberOfLines when the cell is selected to make it grow.
You can try the 3rd library ExpandableLable written by Swift.
Set the custom class of your UILabel to ExpandableLabel and set the desired number of lines and collapsed text:
expandableLabel.numberOfLines = 5
expandableLabel.collapsedAttributedLink = NSAttributedString(string: "more")
expandableLabel.ellipsis = NSAttributedString(string: "...")
// update label expand or collapse state
expandableLabel.collapsed = true
You may need set a delegate to get notified in case the link has been touched.

Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO

I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.
When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.
Thanks!
Ok, per my comment, my solution is to have a surrogate UITextField that has its hidden property set to YES. What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.
I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField
I had a UISearchBar property in my viewController. And I did it like this:
- (void)viewWillAppear:(BOOL)animated
{
[self.searchBar becomeFirstResponder];
}
This should work the same for a UITextField.
As for disabling editing, use:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
You should have set your viewController to be the delegate of UITextField.
Edited answer: Try this:
1. [txtField becomeFirstResponder];
2. txtField.enabled = NO;
3. when some press on keyboard, then txtField.enabled = YES;
Check this out : http://www.youtube.com/watch?v=cKV5csbueHA

Prevent default keyboard from showing when UITextField is pressed

Is there a way to make a custom keyboard to pop up when a user presses on a UITextField. I have a view with a custom keypad on it and I want that to come up when the user presses on the UITextField instead of the default apple keyboard.
Since iOS 3.2 there's a property for exactly that, called inputView.
just go like this: [myTextField setInputView:myInputView] - where myInputView is obviously your custom input view. Then the system will pop up your view instead of the predefined keyboards.
You may set delegate in your UITextField and return NO in textFieldShouldBeginEditing:.
not sure I understand the question? the keyboard type can be assigned to the UITextField so If you want a different one then specify it when initializing the object
UITextField * textFld = [[UITextField alloc] init];
textFld.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIKeyboardType

How can I show a UIDatePicker instead of a keyboard when a user selects a UITextField?

I have a nice clean UI within a table view which has a few text fields for the user to fill out. One of the fields is for the user's birthday.
I'd like to have it so that when the user selects the birthday field, a view containing a UIDatePicker would come up as, just like the different keyboards do when selecting a text field.
Can this be done? I would have to prevent the text field from being the first responder (to avoid having the keyboard come up) and I would have to animate the view sliding up if no keyboard was showing before.
Would presenting the view modally be an option? If so how would I go about doing it? From the documentation it seems that modal views still take up the whole screen, I just want to use the lower 216 pixels (height of the keyboard and UIDatePicker).
Any one have any tips on how to go about doing this?
Old question but the correct way to do this these days would be to set the UITextField's inputView to a picker you created somewhere. Something like this:
UIPickerView *myPicker = [[UIPickerView alloc] init];
// set picker frame, options, etc...
// N.B. origin for the picker's frame should be 0,0
[myTextField setInputView:myPicker];
When you go to edit a UITextField, iOS really just displays whatever view is at textField.inputView which by default is the keyboard, you can make it anything you want as long as it's a subclass of UIView.
Regarding animation, take a look at DateCell sample application -
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
And in any case, the proper way to do this is set UITextField's inputView to show the picker instead of the keyboard. That's what it's meant to do. More on that here:
How can I present a picker view just like the keyboard does?
Cheers,
Oded.
I would implement this by just animating a view containing the UIDatePicker, a Done, and Cancel button) up from the bottom of the screen. Using CoreAnimation, this should be pretty easy.
Why are you using a text field if you don't want to accept user input from a keyboard? Instead use a UILabel subclass (where you override the touchesBegan/Ended:withEvent: set of methods to show the UIDatePicker) or a UIButton (where your action is a method which slides up the UIDatePicker).