I have a textField called titleTextField in which the user can enter the title of their choice before saving a note. What I want to do is automatically fill this field with the text "Untitled" if the user has left this field blank.
I've tried several solutions, but they either do nothing or set the title always to Untitled even if the user has entered text.
This is the closest I've gotten, but it's still not working right:
if(![self.titleTextField.text isEqual:#""]){
self.titleTextField.text = #"Untitled";
}
My thought was that this would check whether the titleTextField is empty, and if so, it would populate the field with the text "Untitled." I've tried applying this in the viewDidLoad, viewWillAppear, and viewWillDisappear -- I really don't care whether the "Untitled" text is populated when the screen loads or after the user is finished with the page; I just want the end result to be an empty title field saving with the text "Untitled".
Is the coding wrong, or am I just applying it in the wrong place?
Solution as per the way you are looking for is :
if([self.titleTextField.text isEqualToString:#""]){
self.titleTextField.text = #"Untitled";
}
You can also use below :
if([self.titleTextField.text length] == 0){
self.titleTextField.text = #"Untitled";
}
Related
I currently have four text fields. At the beginning, only one text field shows. After the user enters text to the first text field and taps a button a new text field is presented and the previous one is hidden with the text field hidden property. My current code only shows the first text field, hides it and shows a new one, and stays in the second text field. There are still two more text fields that need to have the same functionality.
Can you explain why my current code is not working?
Thank you
This is my current code
if nameTextField.text != nil {
nameTextField.resignFirstResponder()
emailTextField.isHidden = false
nameTextField.isHidden = true
emailTextField.becomeFirstResponder()
} else if emailTextField.text != nil {
emailTextField.resignFirstResponder()
emailTextField.isHidden = true
firstPasswordTextField.isHidden = false
firstPasswordTextField.becomeFirstResponder()
} else if firstPasswordTextField.text != nil {
firstPasswordTextField.resignFirstResponder()
firstPasswordTextField.isHidden = true
phoneNumberTextField.isHidden = false
phoneNumberTextField.becomeFirstResponder()
} else if phoneNumberTextField.text != nil {
phoneNumberTextField.resignFirstResponder()
}
Hi Mathew the condition what you have written is incorrect . Lets say user first enter in nameTextField and then you are checking emailTextField not equal to nil.. Sone ones user enter in both the textfield , it is always going to satisfy the first condition only.. Just put a brteakpoint and check your logic.
I am trying to get the text from a UITextView whose entry method was dictation. In the textViewDidChange delegate method, I have tried printing the following info:
print(textView.text)
print(myTextView.text)
print(textView.hasText)
print(textView.attributedText)
print(textView.textStorage)
all of these come back nil, except the hasText comes back as false.
The text is visible right there in the textView, but it is not being registered. I should say that when I go edit another form in the field, and then try to get the value from this text field, then the the text entered IS visible in the textView's textView.text property. But it's like it takes a few moments of editing other fields to fully "register" with the textView object.
Any idea what could be happening here?
Try this after a while
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when) {
print(textView.text)
print(textView.hasText)
print(textView.hasText)
print(textView.attributedText)
print(textView.textStorage)
}
Update: It`s because you are trying to print the text before setting it. It takes a little time to set your text into textView.
My table view contains four textfields
first TextField contains lab name
second textfield contains picker view (with options < ,> , <>, =)
if user selects <> then we are showing two text fields other wise
only one
if selectedValue._conditionSymbol == "<>" {
textField2.isHidden = false
}else {
textField2.isHidden = true
}
this is initial screen
if user click on cancel or close buttons then i need to clear the data in last two text fields.
i have used
textfield2.clear()
textfield1.clear()
tableview.reloadData
but it is not working
after clicking cancel or close button then also getting two textfields.
but i should get pickerview data as "<" and only one texfield should be visible
try this:
textfield2.text = ""
textfield1.text = ""
tableviewOutlet.reloadData
I'm learning OBJ.C/Xcode/Cocoa here and have run into a question I can't seem to find an answer for.
I want the text in a UILabel to switch between two states (ON/OFF for example) each time the user clicks the same button.
I can get the label to switch to one state, but can't get it to switch "back" when the user clicks the button again. I am assuming there is some logic required, or checking the status of the object once the button is clicked...
Does this require me to keep track of a bool or the "state" of the Label (or button)?
Would this require two methods to be associated to the button, or can it happen with just one?
Thanks for any guidance/pointing in the right direction/Code snips!!!!
~Steve
I got an answer, and it works:
- (IBAction)flip:(id)sender
{
_flipButton.selected = !_flipButton.selected;
self.flipLabel.text = (_flipButton.selected) ? #"ON" : #"OFF";
}
- (IBAction)flip:(id)sender {
[yourButton setSelected:!yourButton.selected];
self.flipLabel.text = (yourButton.selected) ? #"ON" : #"OFF";
}
This toggles the selected state of the button then checks its state to determine which string to pass to the text property of your label.
the easiest way (I think) is to store a BOOL with the state of the UILabel and (on each click of the button) negate the BOOL and set the appropriate text of the Label
You could call this: yourLabel.enabled = !yourLabel.enabled on button click to change the enabled-state of your UILabel. Or what kind of state do you mean ?
I seem to be missing something very simple here.
I have a UIViewController which contains a UISegmentControl with two segments ("shown" & "not shown").
The user selects one in this view controller and fills in some information into text fields which all gets saved to a table view controller.
When I click on a cell to edit the information, I can't get the selected segment to show, so if I select "Not shown" in this cell when saving, I want it to show "Not Shown" selected when I edit the cell.
I then of course want to provide the user the ability to change from "Not Shown" to "Shown" with the UISegmentControl.
My code for saving the UISegment Control in the save method of the creating View Controller is:
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 0);
I'm using Core Data here.
So in the detailViewController, I have tried a few things but with no luck (it's always showing the first segment).
if ([contract.wasShown boolValue]) {
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 0);
}
else {
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 1);
}
What do I need to do to get the selected segment shown and then what should I put in the save method of the Detail View to change that selection if possible?
Thanks!
Sorry all - this was just me being stupid.
Implemented with the following code in viewDidLoad:
if ([contract.wasShown boolValue])
{
self.isShownSegment.selectedSegmentIndex = 0;
}
else
{
self.isShownSegment.selectedSegmentIndex = 1;
}