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

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;

Related

UITextbox and Navigationcontroller

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

Changing element in another ViewController

I am new to iOS and have a very basic question. I've googled and can't find the answer.
I have a UILabel on one ViewController and view and would like to change the font color with:
myLabel.textColor = [UIColor blueColor];
but in another ViewController. Is that possible?
So myLabel is in the first ViewController and the button to change the color of myLabel is in the other ViewController.
So more information like yall asked:
My app is compiled of tons of buttons with simple commands. All the buttons are in the same area so I thought of putting some of them on a different view instead of just piling buttons on top of each other and calling .hidden = true; on them.
It is possible but it does not immediately sound like a good design. Without knowing more about your app...
If the color change is to reflect some change in your model you should effect the change through your model:
--(UIEvent)--> ViewController1 --(update)--> model --(observe)--> ViewController2 --(change color)-> textField.
If your app is not that complicated, you should at the very least implement a method on ViewController2 that is descriptive of the reason why the text field should change and send that message (call the method) from ViewController1.
You will also need to pass a reference to VC2 into VC1 - you either already have this because there is a VC1->VC2 parent child relationship or you will have to do it through a joint parent.

How to make IBAction (from button pressed on View1) finish BEFORE View2 viewDidLoad happens?

I'm asking this because I haven't found answer or solution on any other topic, sorry if this question was already asked and answered. THIS didn't answer my question, although problem is similar.
I am trying to explain it as possibly good as I can.
I'm having a singleton containing data used in application, let's call it DataStore. I'm trying to change some value in it, let's say DataStore.x, using one of IBActions (by pressing one of multiple buttons in View1). This button is also responsible for changing view to View2, modal link. viewDidLoad function from View2 is using DataStore.x variable.
Problem :
DataStore.x isn't set by IBAction yet when it's being used by viewDidLoad (using NSLog gave me information that value is being set, but too late).
prepareForSegue function would be perfect if I knew inside it which button was pressed, so before I switch views I can put the proper value to DataStore.x.
Thank you for your time.
Karol

What are the "First Responder" and "Exit" boxes purpose in the storyboard editor?

In the XCode IDE, at the bottom of the view controller in the MainStoryboard editor, are two boxes: First Responder, and Exit.
I know what a firstResponder is programatically within the code, but in the storyboard editor, I can't seem to do anything useful by it.
Am I able to use the first responder in this area to somehow set the first responder of the view? I'd like the first textfield to be active on load and I have tried right+click and dragging to no avail. I know I can set it programatically in the viewDidLoad method, but is there some way of doing it here?
And what is the green Exit for?
There are no good answer for this question, so I am posting my answer:
From here:
Note: You probably won’t be using the First Responder very much. This is a proxy object that refers to whatever object has first responder status at any given time. It was also present in your nibs and you probably never had a need to use it then either. As an example, you can hook up the Touch Up Inside event from a button to First Responder’s cut: selector. If at some point a text field has input focus then you can press that button to make the text field, which is now the first responder, cut its text to the pasteboard.
Edit:
1) First Responder is very useful if you are using text fields with keyboard notifications. I use it to make keyboard disappear, make an outlet to variable currentFirstResponder of your class, and in viewWillDisappear:
[self.currentFirstResponder resignFirstResponder];
2) You can read about unwind segues ("Exit" box) here
I've never used it and probably never will but you can assign an object to be the first in line to receive the events from the UI.
I suppose you could be creating a UIView subclass and add it in to a UIViewController but you actually want some other object to receive and process the events other than the UIViewController you are adding it to.
I found this link which kind of explains it a bit better.
First Responder: The First Responder icon stands for the object that the user is currently interacting with. When a user works with an iOS application, multiple objects could potentially respond to the various gestures or keystrokes that the user creates. The first responder is the object currently in control and interacting with the user. A text field that the user is typing into, for example, would be the first responder until the user moves to another field or control.
Exit: The Exit icon serves a very specific purpose that will come into play only in multiscene applications. When you are creating an app that moves the user between a series of screens, the Exit icon provides a visual means of jumping back to a previous screen. If you have built five scenes that link from one to another and you want to quickly return to the first scene from the fifth, you’ll link from the fifth scene to the first scene’s Exit icon.
More here
You don't see this very often, where a deleted answer is actually correct, and the comment (likely influencing its deletion) on it is totally wrong! I'll try and improve on it.
Usually the IBAction you want to hook up to a button is in the view controller containing the button. However if the IBAction is in a different controller, e.g. a parent controller then drag from the button to the First Responder object and you are able to select the IBAction in the parent controller!
As the hidden answer states, how this is implemented is the action is sent to nil, which has the effect of the responder chain (i.e. view hierarchy) being searched for the action, as follows:
[UIApplication.sharedApplication sendAction:#selector(nextObject:) to:nil from:self forEvent:nil];
An example is a custom UITableViewCell. Add a UIButton to the cell but you want the action to go up to a View Controller that has an embed segue to a UITableViewController. Drag the touch up instead action to the First Responder and select the action in the container view controller. In the action to find the indexPath simply loop the visibleCells and check if the sender is isDescendantOfView:
- (IBAction)cellButtonTapped:(id)sender{
for(UITableViewCell *cell in self.tableViewController.tableView.visibleCells){
if([sender isDescendantOfView:cell]){
NSIndexPath *indexPath = [self.tableViewController.tableView indexPathForCell:cell];
NSLog(#"tapped %#", indexPath);
}
}
}
Another example could be a reload button: say your first view controller shows an downloaded item with an IBAction to reload it to get the latest data, then your child controller shows some detail, but you also want them to be able to reload the main item from within the detail, just add a button in the detail and drag its action to First Responder and select the reload IBAction in the parent controller. This allows you to hook up buttons to parent actions with no additional code like delegate methods!
For this to work the action needs to be in the responder chain hierarchy or it won't be found, you can read how the chain is built up in the docs. Also note if called from code the view needs to have appeared, viewWillAppear is too soon.

show different controllers for one tab depending on some condition

When user taps on Favourites tab, I need to show:
Introduction of how to add favourite, if there is no favourite
Otherwise show the list of favourites
So when user taps on the tab button, I need to check if there is any favourite then decide which controller to use, how do I do this?
Thanks!
If the introduction is static, with no significant user interaction, you can place the introduction into a subview that is on top in the view containing the list of favorites. Set its frame size to cover up the favorites list. (It will be dismissed by the user with the code below.) Call this the introductionView. One way to put the introductionView on top is to add that subview last when you create the list-of-favorites view.
In your view controller for your list of favorites, go to viewWillAppear and add these lines:
(If there are already favorites, this assumes the user has been through the introduction.)
if (*some test whether there are already favorites on the list*)
introductionView.hidden = YES;
else
introductionView.hidden = NO;
Add some code to run when the user presses the dismiss button
- (void) dismissHit{
introductionView.hidden = YES;
}
This will always show the introductionView when the favorites tab is hit, unless there are some favorites already on the list. Read the UIView class reference and the UIViewController class reference. Look at the methods listed to get a better feel what this is doing.
Good luck.

Resources