How to pass a value from one view to another view - iphone

I have two views of two subpages. Here I want to pass the value from one sub class of view to another. I searched the web, but can't find a clear answer. Can anybody provide an example for passing a UITextField value from one to another?

You can maintain a common string in appDelegate.
Define a string
NSString *commString;
in appDelegate.
And then when you submit the uitextfield value just assign that text field value to the
appDelegate.commString
use this in the next view.
ProjectNameAppDelegate *appDelegate = [[UIApplication sharedApplicatoin] delegate];
appDelegate.commString = textField.text;
This will work for sure.

Related

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

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.

Save data from 1 page and add that data to a tableView

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];
}

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];
}

One IBOutlet for two UITextFields

I have two UITextField one for Portrait and other for Lanscape View but I want that editing the first should reflect the changes to other text field too. I have to use separate views for both landscape and portrait. My last option is to make two outlets for them but I want to use only a single one.. Any help would be appreciated.
One option is to make use of IBOutletCollection. You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet). In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags.
There is another workaround. You store the values from the text fields before you use the nib for the changed orientation like
NSString *firstText = firstTextField.text;
NSString *secondText = secondTextField.text;
then after exchanging the nibs use this to populate the text fields in the new nib like
firstTextField.text = firstText;
secondTextField.text = secondText;
You might implement appropriate method(s) in UITextFieldDelegate.....
Such as
//not real code here just off the top of my head
(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSString* updatedText = [textField text];
firstTextField.text = updatedText;
secondTextField.text = updatedText;
return YES;
}
u can use 2 diiferent xib files and do the same thing
To load a new xib file use the following code
[[NSBundle mainBundle] loadNibNamed:#"tests" owner:self options:nil];
u can still use the existing xib file by duplicating it and and loading it with the above code when the orientation changes.
There wont be much changes in the code as u can use the same iboutlets in both the orientations without even requiring to manually link them

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