Putting text input from one view to another

2019-08-28 17:31发布

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)!

4条回答
别忘想泡老子
2楼-- · 2019-08-28 17:45

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"];
查看更多
爷的心禁止访问
3楼-- · 2019-08-28 17:54

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.

查看更多
我只想做你的唯一
4楼-- · 2019-08-28 17:58

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"];
查看更多
男人必须洒脱
5楼-- · 2019-08-28 18:01

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.

查看更多
登录 后发表回答