How do you pass data from one text field to another when using Storyboards in iOS5? - iphone

I am using storyboards for the 1st time, and I cant figure out what I am doing wrong here ... I have a button that transitions from one view controller to another using StoryBoards (the 2nd view is presented modally).
I am trying to use the "prepare for segue" in order to pass the value of a text field from view 1 to view 2, but it is not working. Can somebody tell me what I have wrong here ... ?
View 1:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"saveGame"]) {
statsViewController *svc = (statsViewController *)[segue destinationViewController];
[svc setStatsTextField:gameTextField];
}
}
If there is other code I can post to clarify please let me know.
(for the record there are no errors, the text field on view 2 just doesn't update.)

You cannot just assign a text field in one view controller to a property in another one. This achieves nothing for the text field that is actually in the second view controller's view. Instead, you have to assign a value to the text field's text property. (And ideally, this value should not come directly from another text field because you shouldn't use views to store your app's data. Whenever a text field updates, you should store the updated value in a variable in your view controller or model.)
Also, the statsTextField does not yet exist at the time this code is executed because the destination view controller's view is not yet loaded. You should declare a separate string property in statsViewController (class names should begin with a capital letter btw) and then assign the text field's value in viewDidLoad.

The text field is probably nil at that point since the view hasn't been loaded. You can force this (no problem doing so since its about to happen anyway!) by wrapping your code in an if statement:
if (svc.view)
svc.textfield.text = #"Hello";
Accessing the view property forces the view controller to load the view, if it is not already present.
I notice you seem to be passing a whole textfield object instead of a string to the text property - that doesn't seem like a good idea. It should be more like my example above.

Related

Edit Name of cell upon tap - Swift

I want to edit the text in a cell by tapping and I was wondering if there is a way to do it other than by adding a text field. is there something in the table view delegate that I missed?
You can create cell (or any other UIResponder subclass) that conforms UITextInput protocol (or maybe UIKeyInput is enough). Here is simple example (old link)
Notice,
you should set cell.canBecomeFirstResponder = true.
To show keyboard use cell.becomeFirstResponder()
Maybe there something else, that you should do. I recommend reading docs.
...Or use UITextField :)
We use an array to display no of text label in cell.
so you can do is, you can use a text field and a button on a view.
In button action add a line of code like stringArray[2] = "new string" to replace the particular element from array and when you enter a text and press a button the element will get replaced and just reload the table view.
And you can also write the method in didSelectRowAtIndexPath, write stringArray[indexPath.row] = "new string" and reload the table view and it is done.

How is the 'search for tag' conducted?

In this SO question, a few answers reference searching the 'view hierarchy'. This question seems to indicate that the default is not to search 'branches' first.
Is this a search of the entire view hierarchy of the controller or just subviews of superview (below)?
For instance, assuming there is a button within a subview of the controller's view and that more than one of these buttons (in different subviews) exist and using this code:
- (IBAction)showInfo:(id)sender {
[[sender superview] viewWithTag:2]
}
An alternate question is: Will duplicating the tag number cause the wrong view to be returned (again assuming different branches of the view hierarchy)?
Contained in the UIView Reference for -viewWithTag: is the following:
Return Value
The view in the receiver’s hierarchy whose tag property matches the
value in the tag parameter.
Discussion
This method searches the current view and all of its
subviews for the specified view.

Passing data from one view controller to another without using Segue

I have a UITableView (let’s say class “Parent” which is in view "screen 1"). When I click on one of the Parent’s cell's, I’m displaying another UITableView (let’s call it class “child” in view “Screen2”). Parent and child are connected through “segue” and I’m able to pass the data using “segue”.
For example, parent is having cell – “Cell1” and on touch “Cell1”, I’m getting “Cell11”,”Cell12”,”Cell13” [this is “screen2, table view object”].
Now, there are some descriptions associated with “Cell11” (Once I touched “Cell11”) and i'd like it to display Cell11’s description in another view controller (“screen3”). Here, to pass information between “screen2” to “screen3” I'd rather not to use a “Segue”.
How can I do this?
There are two ways in which you can present a new screen.
1) with the interface builder, here you use a segue to pass something onto the new screen.
2) manually instantiating it, after it is instantiated you can just present it.
Considering you do not want to use method 1, you can use method two like this:
ScreenBClass *screenB = [self.storyboard instantiateViewControllerWithIdentifier:#"screenB"];
screenB.objectPointerInOtherClass = self.objectIWantToPass;
[self screenB animated:YES];

Add data to a table View [duplicate]

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%# "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D
That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.
Change the first line to,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
EDIT
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:#"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.
You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.
It's definitely one of these.
Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.
Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear
You need to make sure that your array is allocated before adding data to it.
NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.
Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath
You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.
EDIT
Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.
example:
in the .h of your UITextField class create a controller reference.
#property(nonatomi,assign)CheckListPracticeViewController *controller;
then when you create your new UITextField controller class pass the reference to its creator throught controller instance.
//in CheckListPracticeViewController.m file
myEdiotr.controller = self;
Later use controller instance to save the text from the UITextField.
- (IBAction)saveButton:(id)sender {
[controller.cells insertObject:textField.text atIndex:0];
[controller.myTableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}

Navigation object

I have a navigation based application. when I click on a table's row next view appears and back button (manually created )appears. Now i want to know how can I get the object of previous view in current view so that I can change one of the label's text of previous view using previous view's object ?
Thanks.
You can pass the value before pushing the second view to the necessary variable
secondViewController.variable = firstViewController.variable;
Then push the secondViewController
Update:
Use a Bool variable willBePoppedBack as class variable and set it as NO initially.
- (void)viewWillAppear {
if(willBePoppedBack)
{
// your label text after pressing back button
}
else {
// your label's default text
}
}
You should set the variable willBePoppedBack to YES when you push to the next view.
If you want to do this you need to make a object of previous view here and then by . operator you can access first page label and write some thing on it.
FirstView *obj=[[[FirstView allo] initWithNibName:#"FirstView"] autorelease];
obj.label.text=#"Text";
Edit:
you need to make a NSString property in appDelegate class then make the object for app delegate and set the value of this string which you want on label
now pop the view and in viewWillAppear use this string to write on the label.
It must work.
Initialize the second view by passing the tableviews object, check the below link, with the similar approach you can achieve your requirement.
navigation based