开发者

taking string from a text field and passing it to the previous viewController

im my app that im tring to build, i want to have a table view, where you select a row e.g email address. the view pushes to a simple page with a uitextField, when you hit the save button it pops the view controller back to the initial page, wh开发者_如何学Cere the user can select the next field.

the issue that i am having is passing the information entered in the textfile back to the first view controller. this should be really simple, but anything i try just does not work

what is the best way to go around this.

thanks


You are probably thinking about the problem backwards. In an MVC system like Cocoa, the job of View Controllers is to manage views, not data. Create a model object to hold the data you're updating. When you create a view controller, pass the model object to it. It may update the model with changes the user makes. It should not worry about who called it, or who it returns to. It should just update the model object, and other interested parties should read the model object. As an example:

  • SettingsViewController would have a model object called Settings
  • When you dive into a detail view controller like EmailViewController, you pass the settings to it like emailViewController.settings = self.settings before presenting it.
  • When the user makes changes, just update the object like self.settings.emailAddress = ...

This separates your view logic from your model logic, which is a key features of Cocoa patterns. If you fight this pattern, you're going to often find yourself thinking "it sure is hard to get there from here."


You can either use a delegate method or, even simpler, just define an instance variable NSString *textEntry in the first view controller that can be set (property/synthesize) and then access that view controller from the stack.

For example, in the pushed view, do something like:

FirstViewController *firstViewController = [[[self navigationController] viewControllers] objectAtIndex:0];

[firstViewController setTextEntry:[textfield text]];


The easiest way I found to do this is using NSNotificationCenter.

In the ViewController with the TableView:

- (void)updateRowValue:(NSNotification *)notification{
     NSDictionary *valuesDictionary = [[NSDictionary alloc] initWithDictionary:   [notification userInfo] copyItems:NO];

     NSString *newString = [valuesDictionary objectForKey:@"StringVal"]
}

This is the method called when row is selected

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     TextFieldViewController *tfvc = [[TextFieldViewController alloc] init];
     [tfvc setPostNotificationString:@"updateRowValue"];
     [self.navigationController pushViewController:tfvc animated:YES];
}

Now in the viewController with the textField, when you press the button to return to the previous viewController call this:

- (IBAction)saveButtonPressed{
     NSArray *valuesArray = [[NSArray alloc] initWithObjects:textField.text,nil];
     NSArray *keyArray = [[NSArray alloc] initWithObjects:@"StringVal",nil];
     NSDictionary *dictionary = [[[NSDictionary alloc] initWithObjects:valuesArray forKeys:keyArray] autorelease];

     [[NSNotificationCenter defaultCenter] postNotificationName:[self postNotificationString] object:self userInfo:dictionary];
     [[self navigationController] popViewControllerAnimated:YES];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜