Putting text input from one view to another

£可爱£侵袭症+ 提交于 2020-01-17 08:20:12

问题


I have tried so many tutorials that I am wondering why I am not getting such a simple problem. I have a view controller called SetBudgetViewController. I have a text field in this view that I have connected as an outlet called *amountToSpend. I have another view used elsewhere in the app that has a label called *amountSet. How do I make the numbers entered into the first text field be displayed in the label in the other view? Thank you all so much (this is driving me mad)!


回答1:


First, declare a property in the other view controller:

@property (strong, nonatomic) NSString *amountToSpend;

In SetBudgetViewController, in your -(void)prepareForSegue method:

if([segue.identifier isEqualToString:@"YourIdentifier"])
{
    OtherViewController *vc = segue.destinationViewController;
    vc.amountToSpend = self.amountToSpend.text;
}

In the other view controller, display the amount in viewDidLoad.

self.amountSet.text = self.amountToSpend;

EDIT 2: Alternative for passing data between VCs not close to each other. You can repeat the action above or use NSUserDefaults.

In SetBudgetViewController after amount is entered:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.amountToSpend.text forKey:@"AmountToSpend"];
[defaults synchronize];

In the other view controller, display the amount in viewDidLoad.

self.amountSet.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"];



回答2:


I hope this will work for you First View where to set User Default

.h File

@property(nonatomic,retain) IBOutlet UITextField *txtfield;

in .m File

  @synthesize txtfield;

Now on click of button

  NSString * text = txtfield.text;
 [NSUserDefault StandardUserDefault] setValue : text forKey : @"textfieldtext"];

the push your view or present modal as you want

then in the next view write as follow

label.text = [NSUserDefaults standardUserDefault]valueForKey :@"textfieldtext"];



回答3:


Since you have lots tutorials, I guess you are enough in coding but not familiar with the concepts..

First of all, since these 2 controls are at different viewcontrollers, that means you have to build a connection between them to help the label find out what u entered in the text input. The connections could be delegate, notification or the plist value, so u will find lots solution here.

And for this scenario u mentioned, I would suggest delegate solution. Since you have tutorials, try to find the delegate section and realize what the delegate does and why.




回答4:


First of all you will need a way to know when the user has entered something in your first textview. One way of doing this is to implement a UITextField delegate and overwrite the textFieldDidBeginEditing method.

Next, you need to get the inputted data and send it to the second textView. A easy way of doing it would be to use NSNotificationCenter. Use it like this in your first view controller:

-(void) textFieldDidBeginEditing:(UITextField *)textField {

    NSArray* objects = [[NSArray alloc] initWithObjects:[textField text], nil];
    NSArray* keys = [[NSArray alloc] initWithObjects:@"text", nil];
    NSDictionary* dict =[[NSDictionary alloc] initWithObjects:objects forKeys:keys];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"firstTextFieldEditted" object:nil userInfo:dict];
}

On your other view controller you need to add this on your init method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifySecondTextField:) name:@"firstTextFieldEditted" object:nil];

And create the following method:

- (void)modifySecondTextField:(NSNotification*) notification {

    NSString* text = [dict objectForKey:@"text"];

    // TODO - update your textfield here

}

If you need to differentiate the sender of the notification, simply do

[[NSNotificationCenter defaultCenter] postNotificationName:@"firstTextFieldEditted" object:textField userInfo:dict]; -- notice object:textField. You can then differentiate them by their tag for example.

This way you have a reference to your initial textfield I hope this gives you a good ideea of how the NSNotificationCenter works. Good luck.



来源:https://stackoverflow.com/questions/17396456/putting-text-input-from-one-view-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!