How to send text from text field to another view controller - iphone

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField and upon tapping the Send button, the value of the UITextField in ComposeViewController will be transferred to the table cell's UILabel in a custom cell in MasterViewController and also to the DetailViewController where another UILabel will get the text value from the UITextField. The DetailViewController is the ViewController loaded when the user taps the cells from the UITableViewCells.
I actually read related articles below but it doesn't work on my end.
How to send the text from textfield to another class?
How to see text from one text field in another text field?
How to send text field value to another class
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
#property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
#property (nonatomic, copy) NSString *messageText;
for DetailVC.m
#synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.

You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
#interface view2:UIViewController {
NSString *str;
}
#property (nonatomic, retain) NSString *str;
in .m file
#synthesize str;
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
For viewing one textfield's text in another use
secondTextField.text = firstTextField.text;
Hope this helped let me know if you are looking for something different.

take one variable in the app delegate like in appdelegate.h
#interface appdelegate
{
NSString *str1;
}
#property (nonatomic, retain) NSString *str;
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
NSString *str = appdel.str(getting value);

Try this one:
#interface SecondView:UIViewController
{
NSString *stringSecond;
}
#property(nonatomic, retain) NSString *str;
In First View u have to create an reference like this:
#import "SecondView.h"
SecondView *detailViewController = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
detailViewController.stringSecond = #"Some string";

Related

DetailViewController programmatically iOS

Im using a custom ModalViewController called MZFormSheetController to display a Detail View for UICollectionView. Currently I have created properties in the modal view controller such as these :
#property (strong,nonatomic) NSString *user;
#property (strong,nonatomic) NSString *caption;
#property (weak, nonatomic) IBOutlet UILabel *username;
#property (weak, nonatomic) IBOutlet UILabel *captiontext;
And I attempt to set the display of the detail view controller when the user taps on the UICollectionViewCell like this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *entry = [self entries][indexPath.row];
NSDictionary *text = [self entries][indexPath.row];
ModalViewController *m = [self.storyboard instantiateViewControllerWithIdentifier:#"modalView"];
m.entry = [self entries][indexPath.row];
m.text = [self entries][indexPath.row];
m.user = entry[#"user"][#"full_name"];
m.caption = text[#"caption"][#"text"];
MZFormSheetController *formSheet = [[MZFormSheetController alloc] initWithViewController:m];
formSheet.transitionStyle = MZFormSheetTransitionStyleDropDown;
formSheet.shouldDismissOnBackgroundViewTap = YES;
[formSheet presentAnimated:YES completionHandler:^(UIViewController *presentedFSViewController) {
}];
formSheet.didTapOnBackgroundViewCompletionHandler = ^(CGPoint location)
{
};
}
I have created two labels in storyboard for the modalviewcontroller and I attempt to make them equal the caption and user values from the MainViewController like this
[self.username.text isEqualToString:self.user];
[self.captiontext.text isEqualToString:self.caption];
However after all this the labels of the modal view controller still say label like this..
Your labels are not being updated to the correct values because the -isEqualToString: method is used to test whether two NSStrings are equal, and it doesn't actually set the value of any NSString. If you want to assign a value to a string variable, you do it the same as you would assign any other variable. Therefore, in order to set the labels properly, you just need to assign the text property of the UILabels, like this:
self.username.text = self.user;
self.captiontext.text = self.caption;
Did you set username and captiontext inside your ModalViewController in viewDidLoad method ? After init from storyboard, all IBOutlets are nil, you need to setup then inside viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
self.username.text = self.user;
self.captiontext.text = self.caption;
}

set UITextField.text from another class

I have two views, and i'm trying to show text that i getting from first view in UITextField of another . Second view shown by - (source) so methods ViewWillAppear and ViewDidLoad won't work. And viewDidLoad method of second view is runs when app is started.
I'm tried to make method of second class
secondClass.h:
#property (strong, nonatomic) IBOutlet UITextField *itemName;//all hooked up in storyboard
-(void)SetName:(NSString *)name;
secondClass.m:
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = name;//itemName - textField
}
and use it in first one:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
[secondView SetName:#"Bill"];
NSlog shows "Bill" but textField.text won't change anything.
My guess that app shows UITextField without changes because it shows second view that it gets from viewDidLoad method and i need to update it somehow
My question: What is the best approach to change attributes of UI elements from different classes?
Easiest way:
secondViewConroller.h :
#property NSString * conversationName;
secondViewConroller.m :
#synthesize conversationName;
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = conversationName
}
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
conversationName = #"Set this text";
[secondView SetName:#"Bill"];
I would suggest you to read about Protocols after that.
Easiest way:
in
secondViewConroller.h :
#property (nonatomic, retain) NSString *stringName;
secondViewConroller.m :
#synthesize stringName;
and in viewDidLoad method you write this line
itemName.text = stringName
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
secondView.stringName = #"Set this text";
guess there is something wrong with your itemName variable if it appears to get to the nslog.
did you create a referencing outlet in the interface builder for the textfield?
otherwise you can get the right textfield by tag, in IB put for instance tag 1 on the textfield and do in code:
UITextField *tf=(UITextField*)[self.view viewWithTag:1];
tf.text=name;
(replace self.view for the view holding the textfield, if not directly in the main view)
So i found a solution: There's was something wrong with calling method SetName: with parameters that i getting from first UIViewController.
Basically the solution is : create NSObject and put in there value from first UIViewConroller and then use it in second.
This TUTORIAL helped me to resolve the problem.

Accessing variable values from one view in another

I'm having some trouble understanding how variable values are passed from one view to another. I have a UITextField in the firstview that the user enters a number into. When the user taps a button, that number is multiplied by 2 and the result is displayed on a UILabel in the second view. This is what I have thus far
FirstViewController.h
#interface FirstViewController : UIViewController{
UITextField *numberTextField;
NSNumber *aNumber;
}
#property (nonatomic, retain) IBOutlet UITextField *numberTextField;
#property (nonatomic) NSNumber *aNumber;
-(IBAction)calculate;
#end
FirstViewController.m
#implementation FirstViewController
#synthesize numberTextField, aNumber;
-(double)doubleNumber{
double number = [numberTextField.text doubleValue] * 2;
return number;
}
-(IBAction)calculate{
self.aNumber = [NSNumber numberWithDouble:[self doubleNumber]];
}
//more default code continues below
SecondViewController.h
#import "FirstViewController.h"
#interface SecondViewController : FirstViewController{
UILabel *numberLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *numberLabel;
#end
SecondViewController.m
#implementation SecondViewController
#synthesize numberLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
numberLabel.text = [NSString stringWithFormat:#"%#",aNumber];
}
Best and Easy Way to store value globally
set you object with your keyword
[[NSUserDefaults standardUserDefaults] setObject:#"Ajay" forKey:#"name"];
than get that object any where in you project
NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:#"name"];
You should accomplish what you want by using a segue. Create a segue in your storyboard and then call it in your firstviewcontroller with - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. Then to pass data, import your secondviewcontroller.h file with a property for the value you want to pass and setup - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender.
In that method you can pass things by using [segue.destinationViewController ###call the setter for the property in your secondViewController.h file###];
If this isn't clear, or you need more help just let me know.
I'm assuming that the FirstViewController is instantiating the SecondViewController. If that is so, then you just pass aNumber to the SecondViewController using an additional property:
// add an additional property to the SecondViewController
#property (nonatomic, retain) NSNumber *aNumber;
When you instantiate the SecondViewController inside the FirstViewController, you just pass that value to the SecondViewController before you load it:
// inside FirstViewController
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.aNumber = aNumber;
// inside SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
numberLabel.text = self.aNumber;
}
Mmm... Pay attention to not confuse views and viewControllers!!!
A viewController can manage more than a view. For example in your code you have a UITextField, a UILabel and probably a UIButton. These are all views that are managed by two viewsController (FirstViewController and SecondViewController).
As long as you have so few views to work with you can use just one viewController and pass the value you want to your UILabel directly:
- (void)calculateAndPassValue
{
aNumber = [NSNumber numberWithDouble:[self doubleNumber]];
numberLabel.text = [NSString stringWithFormat:#"%#",aNumber];
}
Otherwise, if your goal is passing variable values from one viewController to another. Well... There are many ways you can obtain that, for example:
Creating an ivar.
Using a singleton.
Saving your data in NSUserDefault.
Creating a database on disk.
First and second cases are good if you need to manage your data while your app is running. Third and fourth if you want to memorize your data for future use and retrieve them at next start up.
Try to search keys like ivar, singleton, NSUserDefault and you'll find many discussions and lines of sample code.

Can't pass UITableViewCell didSelectRowAtIndexPath data to another view controller

I have currently got a UITableView in my main view controller. I want the data of the selected row to be passed to another view controller in my project.
Here's what I have got so far, although there is an error. Why is this? I have referenced the class and the header file in my .h file.
I would really appreciate some help with this as I've tried everything I can think of.
Simply synthesize the object in the destination view.
It will work fine...
like:
in .h
#property(nonatomic, retain) UILabel *note;
in .m
#synthesize note;
Very Simple,I think note is a UILabel,
according to You error
please use this code in AddNote. h file
NSString *noteStr; //Ios 4
IBoutlet UILabel *note;
#property(nonatomic, retain) NSString *noteStr; //Ios 4
#property(nonatomic, retain) UILabel *note;
#property(nonatomic, strong) NSString *noteStr;
#property(nonatomic, strong) UILabel *note; //Ios 5
In AddNote. .m file
#synthesize note,noteStr;
self.note.text=noteStr; //in ViewWillAppear
Some Time Application is crash Because Memory IF you Work In Ios 4 Please Correct this code
an.noteStr - [selectNote retain];
Declare static variable for global use.
Using keyword extern
Create Common.h
extern NSMutableArray *gMyBasketCollectionList;
common.m
NSMutableArray *gMyBasketCollectionList;
Use This Mutable Aarray to Store Your Data and Display in Any other
include common.h in First View And SecondView
Add Object to mutable Array in First View And
Show SecondView And Display access that Array

Program to pass string from one view to another?

I have an iphone application containing two views
The first view have a button, now
i want to show the title of the button on the second view when that button is clicked.
How can i do it because if we save the title into a string ,the string get emptied when second view is loaded ?
In the interface declare the string and make a property:
#interface MyViewController : UIViewController {
NSString *string_i_want_to_set;
}
#property (nonatomic, retain) NSString *string_i_want_to_set;
#end
In the implementation synthesize the string after #implementation MyViewController like this:
#import "MyViewController.h"
#implementation MyViewController
#synthesize string_i_want_to_set;
// OTHER CODE HERE...
#end
Then when you alloc/init the view controller you can set the property like this:
MyViewController *myVC = [[MyViewController alloc] init];
myVC.string_i_want_to_set = #"...."
Hope this gives you more detail.
Thanks
James
you can make object of second view and can pass string to it
view2object.view2string=#"anything" or label.text;