I have a text field in my master view controller.. And I am using FPPopover to display another view controller on top of my master view controller.. I wanna get the name of the user from FPPopoverController and put in a text field in my master view controller. I tried the followings but didnt work..

 MasterViewController *vc = [[MasterViewController alloc] init];
  vc.nameTextField.text = self.nameField.text;


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    MasterViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MasterViewController"];

    vc.nameTextField.text = self.nameField.text;

What else can I try? Thanks.

有帮助吗?

解决方案

Delegation is the best method I think but the easiest way is using notifications.

MyPopoverViewController implementation

- (void)doneButtonPressed:(id)sender{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification"
                                                        object:self
                                                      userInfo:@{@"aKey":self.textfield.text} ];
}

In your master view controller viewdidload method

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

and lastly add this method..

- (void)updateTextField:(NSNotification *)notification {
     self.nameTextField.text = [notification.userInfo objectForKey:@"aKey"];
}

其他提示

I would simply use delegation here. For example you could set up a simple protocol, your popover view controller should have a delegate which could be your main view controller, you implement the protocol method on the main view controller.

When you create the popover controller, make sure you assign the main view controller as a delegate for that controller for example:

Protocol:

@protocol MyPopoverDelegate <NSObject>

@required
- (void)popoverDidFinishTypingName:(MyPopoverViewController *)myPopover;

@end

Main view controller:

- (void)popoverDidFinishTypingName:(MyPopoverViewController *)myPopover{
    self.nameTextField.text = myPopover.textField.text;
    // dismiss the popover here.
}

MyPopoverViewController header:

@interface MyPopoverViewController

@property(weak) id<MyPopoverDelegate> delegate;
@property(weak) UITextField *textField;

@end

MyPopoverViewController implementation excerpt:

@implementation MyPopoverViewController

- (void)doneButtonPressed:(id)sender{
    if(!self.delegate)
        return;
    [self.delegate popoverDidFinishTypingName:self];
}

@end

When you create your popover from the main view controller you would need to set yourself as the delegate:

MyPopoverDelegate *popoverController = [[MyPopoverDelegate alloc] init];
popoverController.delegate = self;
[...]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top