1

I have a ViewController1 with a UILabel that can present a ViewController2 with a Modal Segue. I have a UITextField in ViewController2 that I need to access from ViewController1 so I can set my UILabel with the collected text.

I've tried working with the prepareForSegue without success. What should I do?

EDIT:

I'm using a Delegate, but I'm doing something wrong. Here's the code I'm using in ViewController2.h:

@class ViewController2;

@protocol VCProtocol

-(void)setName:(NSString *)name;

@end

@interface ViewController2 : UIViewController

@property (nonatomic, weak) id<VCProtocol> delegate;
@property (strong, nonatomic) IBOutlet UITextField *nameField;

- (IBAction)setButton:(id)sender

@end

ViewController2.m

-(IBAction)setButton:(id)sender
{
    [self.delegate setName:nameField.text];
}

I conform to VCProtocol in my ViewController1.h. Then, in my ViewController1.m, I have this code:

- (void)setName:(NSString *)name
{
    self.firstSignatureNameLabel.text = name;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqual:@"Sign"])
    {
        ViewController2 *VC = [segue destinationViewController];
        VC.delegate = self;
    }
}
Cody Winton
  • 2,989
  • 5
  • 25
  • 48
  • If you want it to be accessible to every view, you need to create a Model/Data class. The question is not a duplicate, but the answer applies from [this question](http://stackoverflow.com/a/6067515/2535467). [This question might be more like yours](http://stackoverflow.com/questions/2228242/how-to-access-string-variable-in-one-view-controller-to-another-view-controller) – CaptJak Aug 10 '13 at 02:25
  • 2
    This is what delegates are for. VC1 should set itself as the delegate of VC2 when it does the presentation. VC2 should define a protocol with a single method that you would call on the delegate in the text field's action method. – rdelmar Aug 10 '13 at 04:06

2 Answers2

0

You can create a protocol and set VC1 as delegate of VC2, and using prepareForSegue to set VC1 as VC2's delegate should work. I know that you said it didn't work, but I can't see why. Have a try with this :

Give an identifier to your segue (on storyboard), and implement prepareForSegue as shown below :

VC2Delegate.h :

@protocol VC2Delegate
    - (void)updateLabel:(NSString *)text;
@end

VC1.h :

#import "VC2Delegate.h"
@interface VC1 : UIViewController <VC2Delegate>
    // All your stuff
@end

VC1.m :

- (void)updateLabel:(NSString *)text {
    [_label setText:text];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"YourSegueIdentifier"]) {
        VC2 * vc = [segue destinationViewController];
        [vc2 setDelegate:self];
    }
}

VC2.h :

#import "VC2Delegate.h"
@interface VC2 : UIViewController
    @property (weak, nonatomic) id<VC2Delegate>delegate;
@end

VC2.m

- (void)textWasUpdated { // or whatever method where you detect the text has been changed
    if (_delegate)
        [_delegate updateLabel:[_textView text]];
}

Tell me if it works. Else, is prepareForSegue even been called ?

EDIT : Updated my answer (wasn't exactly what you needed). But as you say it doesn't work :

  • Is prepareForSegue called ?
  • If so, is the delegate method called ?
  • If the delegate method isn't called, check that delegate is not nil.

You might want to remove the segue, and present it modally by yourself, using presentViewController:animated:completion:, like that :

- (IBAction)buttonWasTapped {
    static NSString * const idModalView = @"modalView";
    static NSString * const storyBoardName = @"MainStoryBoard"
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
    VC2 * vc = [storyboard instantiateViewControllerWithIdentifier:idModalView];
    [vc setDelegate:self];
    [self.navigationController presentViewController:vc2 animated:YES completion:nil];
}
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
-2

When I ran into this problem, I chose the singleton approach and it works.

Community
  • 1
  • 1
ipatch
  • 3,933
  • 8
  • 60
  • 99