UITextbox and Navigationcontroller - ios

I'm creating an iOS app with swift and XCode 6. I have a multiple page app with a navigation controller. So far so good. When I enter text in one of the subpages go back to the front and reenter this page the text is gone.
My question to you is how to edit the code, so that the text stays in the text box and does not disappear. I know to somehow create a database but thats all I found so far.
Thank you a lot in advance.

When you go "back" you essentially pop the viewcontroller from the navigationstack. At that point there are no more references to that viewcontroller and it get's deallocated and as a result you loose all your changes.
So if u want to save your text you can store it in NSUserDefaults
NSUserDefaults.standardUserDefaults().setObject(textField.text, forKey:"key")
And in viewDidLoad of specific viewController where you want to populate UItextField fetch it like
textField.text = NSUserDefaults.standardUserDefaults().objectForKey("key") as! String

Related

How to reset ViewController on the press of a button in iOS

My app has a ViewController consisting multiple textfield and custom UI elements arranged as a form with "submit" and "reset" buttons.
I want to reset all the textfields and custom UI elements when user clicks the submit button so that user gets a feeling that same Form is opened again.
I tried calling ViewDidLoad() and setNeedsDisplay() on the click of "Submit" button but data previously filled by user remains as it is.
Kindly Help!
Just set the text of all textfields to an empty string (the text value). There is no magic function to clean everything in a view.
The only other solution would be to actually display the ViewController again, but this is probably not what you want, because it causes overhead as well as you might see something of the switching on the screen.
If you programmatically coded your textfields and some other objects, you can refresh always your viewController everytime by adding all your UI codes in the predefined method: -(void)viewWillAppear:(BOOL)animated{} and you should remove all self.view elements by adding this code.
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
You codes in the viewWillAppear will look like this.
-(void)viewWillAppear:(BOOL)animated{
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Codes for User Interface here
}

Objective C: show textfield and textview input in label on next view?

So i've seen many questions alike mine, but all don't have the working answer, or have vague answers (and none of them marked as correct).
I have two ViewControllers:
ViewController #1 has 4 textFields to which there are pickerViews attached in order to choose an option to fill in the textfield.
Also there is 1 textView in which the user can use the keyboard.
To sketch the scenario: Label states: 'Pick a color:' User clicks on the textfield (pickerView shows up full of names of colors) user selects 'red' and the picked choice shows up in the textfield below the label: 'pick a color'.
Then when all textfields and textview are filled in, the user clicks save. In which the save button redirects the user to the 2nd ViewController.
ViewController #2:
This is where i want the input (of the textFields and textView) to be shown in the labels(if this is the correct usage). Since this is where the user will see list of the chosen answers. However i cannot manage to get this working.
What do i need to do in order to achieve this?
Also i'm still learning. Please bear with me.
Thanks in advance.
In general this site is not well-suited to "Tell me how to do XYZ" type questions. It's intended for getting help with code you have already written. You're new, though, so I'll take pity on you.
You should post the code that you have, showing how you get from view controller 1 to view controller 2.
I'm going to assume that you have a segue from view controller 1 to view controller 2.
(You should probably have code that disables your send button until all 4 text fields are filled in with valid colors.)
Anyway, let's assume that your send button triggers a segue, either directly, or in an IBAction that triggers a segue through a call to performSegueWithIdentifier.
You need to define properties for your 4 color strings in view controller 2.
Then, add a prepareForSegue method to view controller 1 if you don't have one already. In prepareForSegue, make sure the segue matches the segue identifier that invokes view controller 2 (good idea even if you only have 1 segue now, since you might add more segues later.)
ViewController2 *vc2 = (ViewController2 *) segue.destinationViewController;
Assuming it's the right segue, get a pointer to the destination view controller and cast it to the type of ViewController2 so you can refer to your color properties.
Then use code like this to pass the colors to view controller 2:
vc2.color1 = myColor1TextField.text;
vc2.color2 = myColor2TextField.text;
vc2.color3 = myColor3TextField.text;
vc2.color4 = myColor4TextField.text;

implementing textViewDidChange:

So, I have this very simple list project with objective-c, where every item on the list should have a title and a description. When clicking on one of the items, the detail view of the item shows up. The title, on the navigation bar is the item's title, and the rest of the view is the item's description. By clicking on the title, the user should be able to change it, and by clicking on the description they should also be able to change it.
Up until changing the title everything is going OK, I've managed to do that. The problem is when it comes to changing the description. I tried using the delegate, but I guess I must be using some command wrong, because it's simply not working. I tried looking for it online, but all I find are solutions for the TextField, which doesn't really help me, since I want to display a possibly multiline description, and I would rather not have to change from a TextView to a TextField every time the user wants to edit it. So if anyone could help me out with that, that would be awesome.
Here is the code where I try using the delegate.
on viewDidLoad I have this:
[self.descriptionLabel setDelegate:self.descriptionLabel.delegate];
and then I have this, where it should perform the actions after the text is changed:
- (void)textViewDidChange:(UITextView *)textView {
NSLog(#"Hello World");
}
P.S.: I was also looking for a way to have the "Done" button show up over the keyboard, because as of now, once the keyboard is brought up, the only way to get it down again is going back to the table view and then into the detail view again. I tried a couple of things already, but I'll admit that I wasn't too thorough on my research. So, as a side note, if you could show that too, it would be great, otherwise, the one thing I'm really after here is the question above. Thanks for any help! :]
Summary of answer based on the comments.
The line:
[self.descriptionLabel setDelegate:self.descriptionLabel.delegate];
needs to be:
self.descriptionLabel.delegate = self;
You want the view controller to be the delegate since the view controller implements the text view delegate methods.
You also need to indicate that the view controller conforms to the UITextViewDelegate protocol.
In your .m file, add the following:
#interface DetailViewController () <UITextViewDelegate>
#end

How to clear text fields when tab is changed?

Hello I have a tabbed application.
It's two calculators. However when I change the tab and go back to it, the data that I entered into the text field remains the same. How do I clear it?
Try this for clear textfield add this line in your viewDidUnload or viewWillDisappear method
yourtextfield.text=#"";
Hope this code is useful for you.

How do I jump to a UIViewController in a storyboard and still retain the navigation hierarchy?

I'm working on a storyboard-based project for ios5, and it is working well so far, but I want to save the user's state when they exit so that when they return to the app, I can jump them back to the place in the storyboard where they were when they left.
My understanding is that as you navigate through a storyboard, it creates a stack of UIViewControllers, so that when you dismiss the current controller, it knows which one to display. But if I jump to one of the UIViewControllers in a storyboard programmatically when the app starts up, it would not have that history, that stack of controllers, to backtrack along. If nothing else, if two controllers segue into the same controller at some point in the storyboard, it wouldn't know which to go back to.
What is the proper way of dealing with this? Do I just go through the storyboard to the point where the user left off, presenting each UIViewController with animations set to NO until I get to the one that the user was on? (And if so, what's the best way to store that information? Are there any methods/classes that can assist with this, or am I rolling my own way of storing their through the storyboard?)
One option is using NSUserDefaults. You can't exactly save the exact instances of UIViewControllers on the disk, and retrieve them as if they were in the memory. What happens is that, you should somehow, e.g. key/value, identify each of your viewControllers that you are interested and want to save their state. Also you need to key each element you want to save its state, and then save using NSUserDefaults.
For exmaple, let's say you have a hierarchy like this:
HomeView > ListOfNotes > NoteAtIndexPath:x
And the user is viewing NoteAtIndexPath:x, and also let's assume the font size is dynamic, so user can make it bigger or smaller at the time of viewing. You can define two keys: IndexOfLastNoteViewed and LasFontSizeViewed, and save them:
[[NSUserDefaults standardUser] setInteger:5 forKey:IndexOfLastNoteViewed];
[[NSUserDefaults standardUser] setFloat:17.0 forKey:LasFontSizeViewed];
When the application launches again, you can check the keys, and browse programmatically to HomeView > ListOfNotes > NoteAtIndexPath:5.
Here are some links that might help you:
Application states
App States and Multitasking
Beginning IPhone Development: Exploring the IPhone SDK By Dave Mark, Jeff LaMarche
-- Application Settings and User Defaults

Resources