I am writing a program where I where the first time a user runs it - they will have to fill out about 10 different UITextFields. I am trying to save the fields so that on subsequent runs of the program that whatever they previously put will already be displayed in those UITextFields so the wont have to re-input it in - unless they want to edit something in which case they still have that option. I think that I have figured out a good way to save the strings using NSUserDefaults but now I am trying to figure out how to have those fields populate a UITextField - it doesnt seem as easy as if they were UILabels. This is the route I am attempting:
// in the viewDidLoad portion.
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; //Hooks.
NSString *placeHolderName = [userData stringForKey:#"name"];
txtName.text = #"%#", placeHolderName;
When I do this, it simply displays the '%#' in the textfields. I want whatever variable being held by placeHolderName to be automatically put into that UITextField. Is this possible?
Just use:
txtName.text = placeHolderName;
or
txtName.text = [NSString stringWithFormat:#"%#", placeHolderName];
In code you have now you accidentally use Comma operator that evaluates its first expression (txtName.text = #"%#"), discards the result and returns the 2nd (placeHolderName)
If I try to set a text of a UITextField in viewDidLoad, the UITextField is always nil, thus nothing happens. When should the
txtName.text = placeHolderName
be called, if I need to populate the text field with the default value when the UIViewController loads?
Related
I am having a trouble on exporting text data. I have a view contains over 3 different uitextview that allow user to type in data. Then i want to export these texts from the 3 different uitextview (e.g. textview1, textview2 and textview3) into a new uiviewcontroller that contains only one uitextview. I also want the textview 1-3 are placed in order. So that the new uitextview will have three different sections in the order of 1-3. I am using PrepareFroSegue method. How should i code this? Many thanks!
What you want to do is concatenate (combine) the strings into one NSString that will be sent into the next view. You can do this in your PrepareForSegue. Put this in your prepareforsegue but edit the code so it works with your code.
NSString *concatenatedString = [NSString stringWithFormat:#"%# %# %#", textview1.text, textview2.text, textview3.text];
YourViewController *dV = [segue destinationViewController];
dV.stringToStoreValue = concatenatedString;
The first line I concatenate the string
The second line I get the destination view controller from the segue
The third line I set the text to a NSString #property in the destination view controller. You must then in the view did load of the destination view controller set
yourcombinedtextfield.text = stringToStoreValue;
I do not directly set the value of the text field in the prepareForSegue because if the view has not loaded yet then the I would be setting nothing and not see the string on the next page in the textfield!
You're trying to communicate between ViewControllers this answer looks like a good starting point for understanding how this is done..
UPDATE :
newController.textView.text = [NSString stringWithFormat:#"%# %# %#", textView1.text, textView2.text, textView3.text];
This question is a bit related to one I posed previously entitled "Recognizing UIButton from a loop".
In this case, I have generated a bunch of UITextFields with a loop. When the value of one of them is changed, I'm able to recognize which textfield it was.
However, I want to then edit every textfield that was generated. Specifically, I get input from the one textfield that was edited, and I want to recalculate the other textfields based on the input and name of the recognized textfield.
How do I call for every one of the other generated, non-edited textfields to be modified?
/Vlad
Since you are already using tags, this is what viewWithTag is used for:
// Get a reference to the textfield with tag 3
UITextField *textField3 = (UITextField *)[self.view viewWithTag:3];
// Calculate your new value
float result = 4.32; // Calculate the value that you want the textfield with tag 3 to display
// Change the contents
textField3.text = [NSString stringWithFormat:#"%f", result];
store the text fields in an array, then when you want to change the value
[self.myTextFieldArray enumerateObjectsUsingBlock:^(UITextField *textField, NSUInteger idx, BOOL *stop){
if (![textField isEqual:theTextFieldThatWasEdited])
textField.text = #"whatever text you want";
}];
I am doing parsing, in my parsearray having only two data number of alerts and number of events these are fine. When we are finding the value like
NSString *alertcount = [[xmlparseArray objectAtIndex:indexPath.row ]objectForKey:#"alerts"];
and assigning these string value into label
mylabel.text = [NSString stringWithFormat:#"%#", alertcount];
same for Events.
then it becomes crash. It says:
index 0 beyond out of empty Array
And these parsing I am using in #RootViewController:
UITableViewController {
}
not using custom cell. In this rootviewcontroller class we are using 7 row(these are constant) and I want to assign at row number 5 the value of alert (i.e. mylabel.text = [NSString stringWithFormat:#"%#", alertcount]) at the right position of cell number 5 same is cell number 6.
Your xmlparseArray might be empty. Check if you have any elements in the array. Pls show the code where you populate the array so that we can have a better idea of where you are going wrong
I'm trying to get the contents from a dictionary into a UITextView. The dictionary contains molecular masses paired with percentages, for example:
24 -> 98
25 -> 1.9
26 -> 0.1
I have an NSArray containing the keys from the dictionary, sorted in ascending order. So, here is my code to generate the string to set as the textField.text property:
-(void)detailIsotopes:(NSMutableDictionary *)isotopes withOrder:(NSMutableArray *)order{
NSMutableString *detailString = [[NSMutableString alloc] init];
for (NSNumber *mass in order){
[detailString appendString:[NSString stringWithFormat:#"%d: %f\n", [mass integerValue], [[isotopes valueForKey:[NSString stringWithFormat:#"%#", mass]] floatValue]]];
}
NSLog(#"%#", detailString);
textField.text = detailString;
[detailString release];
}
This should create a string looking like this:
24: 89
25: 1.9
26: 0.1
For some reason, this method never does anything the first time it runs. I see the NSLog output, which outputs the correct string. However, the contents of the UITextView don't change: they stay as 'Lorem ipsum dolor sit amet...' from Interface Builder. If I run the method again, it works, sort of.
The UITextView displays some of the text, and then just cuts off half way through a line, leaving only the tops of the characters. If I delete the contents above the half line, the other lines pull up from under the divide: the contents are there, they just stop being shown, if you understand what I mean. This appears to go away if I enable paging in the view. If I do that, then the line isn't truncated, but the UITextView just stops showing any content after some point, although the scroll bar indicates that there is more to go (which there is).
The view containing the UITextView is not visible when the contents is set, if that makes a difference. A separate view controller generates the NSMutableDictionary and NSMutableArray and sends them to its delegate, which then sends them to the view which should display the UITextField and has the detailIsotopes: withOrder: method. The two can be swapped between with an info button.
Does anyone understand why these things are happening?
Thanks for any advice you can give!
First of all, I don't think you need to allocate and release your NSMutableString here. Simply initialize one using [NSMutableString string] which creates an empty string you can modify and don't need to explicitly release.
Second thing, you seem to store masses in NSStrings in your NSDictionnary, why do you use their integerValue method for stringWithFormat (with %d modifier), instead of using them as is? (the stringWithFormat modifier for NSStrings is %#)
Also, you talk about a UITextView at start, then about a UITextField, are you sure you did not make a mistake somewhere? I guess the receiving object for your formatted NSString should rather be the UITextView (if you have both a textField and textView).
If it's not about this, maybe you are calling detailIsotopes too early and the textView it's created yet. Try to NSLog its address and see if it's nil the first time. It could be the case if you use Interface Builder and your UITextField is an ib outlet. If you do, then you could store your dictionary and array in the viewController, and set the textField in the viewController's viewDidLoad method. Or call detailIsotopes after you've displayed the view, I guess that's up to you.
About the truncated text, I think that's because UITextView doesn't resize itself automatically, so it keeps the height you originally set. What I usually do is this:
CGRect frame = textView.frame;
frame.size.height = textView.contentSize; // you can adjust this to leave some space at the end
textView.frame = frame;
This will set the textView height to the content (the text) height.
Also note that if your textView is supposed to display the whole text, you can set its scrollingEnabled property to FALSE so it never allows scrolling.
Hope that helps.
I have looked at other answers and the docs. Maybe I am missing something, or maybe I have another issue. I am trying to save a number on exiting the app, and then when the app is loaded I want to check if this value exists and take action accordingly. This is what I have tried:
To save on exiting:
- (void)applicationWillTerminate: (UIApplication *) application
{
double save = [label.text doubleValue]; // This could be the issue
//double save = 3.5; // This works, it saves the value and loads it fine, so that is not the problem here.
[[NSUserDefaults standardUserDefaults] setDouble: save forKey: #"savedNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
To check:
- (IBAction)buttonclickSkip{
double save = [[NSUserDefaults standardUserDefaults] doubleForKey: #"savedNumber"];
if (save == 0) {
[self performSelector:#selector(displayAlert) withObject:nil];
test.enabled = YES;
test.alpha = 1.0;
skip.enabled = NO;
skip.alpha = 0.0;
}
else {
label.text = [NSString stringWithFormat:#"%.1f %%", save];
}
}
The problem is I always get my alert message displayed, the saved value is not put into the label so somehow == 0 is always true. Why would:
double save = [label.text doubleValue];
always equal zero? Before I close the app the number in that label is roughly 0.5% (it varies). If it makes any difference I am testing this on the iPhone simulator.
Many thanks,
Stu
The fact that you can hard-code the value and fetch it back means the problem definitely revolves around your interaction with the label.text and not your use of NSUserDefaults.
Make sure that the label has not already been destroyed at the time you go to fetch its value. As the application is terminating it may have already brought down the view from which you are fetching the value.
Another thing to try would be to get the actual text itself instead of asking the OS to convert the text value into a number first. If you print that out you may get some clue as to what is going on.
Make sure that your applicationWillTerminate: implementation is in your app delegate class.
My guess would be that the text in your label is not a valid double value. From the Apple docs for NSString -doubleValue:
Returns 0.0 if the receiver doesn’t
begin with a valid text representation
of a floating-point number.
Make sure you are passing it something like #"13.2". The best way to check this is to stick a NSLog call right after you create the variable save.