Question

I have two view controller ...

The ViewController 1 has a label which should show the text entered through a TextView.

the ViewController 2 has a TextView that passes the text to the label of the ViewController 1

To do this I used the protocols in this way

Header File VC2.h

@protocol TransferTextViewDelegate <NSObject>
-(void)PassedData:(NSString *)text;

@end

@interface VC2 : UIViewController <UITextViewDelegate>

@property (nonatomic, weak)id <TransferTextViewDelegate> delegate;

@property (strong, nonatomic) IBOutlet UITextView *FFTextView;
@property (strong, nonatomic) NSString *title;

- (IBAction)sendTextViewcontent:(id)sender;

@end

In the Implementation File VC2.m

#import "VC2.h"
#import "VC1.h"

@interface VC2 ()

@end

@implementation VC2
@synthesize FFTextView;
@synthesize delegate;
@synthesize title;

- (void)viewDidLoad {
    [super viewDidLoad];
    [FFTextView becomeFirstResponder];
    FFTextView.delegate = self;
}

- (IBAction)sendTextViewcontent:(id)sender {

    if (delegate) {
        title = FFTextView.text;
        [delegate PassedData:title];
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end

In the ViewController 1, like I said, this is a UILabel which should show the text entered in the TextView of the ViewController 2

Implementation File VC1.m

-(void)viewWillAppear:(BOOL)animated {
    TitoloAnnuncio.text = TitoloAnnuncioInserito;
}

-(void)PassedData:(NSString *)text {   
    TitoloAnnuncioInserito = text;
}

In The Header File VC1.h implemented this property:

@property (strong, nonatomic) IBOutlet UILabel *TitoloAnnuncio;
@property (strong, nonatomic) NSString *TitoloAnnuncioInserito;

My problem is that I can not understand why my label does not show the text .. It remains empty ... I can not pass data from ViewController 2 in 1 ViewController Can you help?

Was it helpful?

Solution

Leaving aside the issue of naming variables with an initial lower case letter...

You are setting a string value in the delegate method, however, iOS is not OS X, there are not bindings, so just changing the value of the property does not automatically change the value displayed in the textfield.

You have a couple of options, one is to update the display in the delegate method.

- (void)PassedData:(NSString *)text {   
    TitoloAnnuncioInserito = text;
    TitoloAnnuncio.text = TitoloAnnuncioInserito;
}

Another way is to set the display in the label whenever the property value changes - which is a bit more robust.

- setTitoloAnnuncioInserito:(NSString *)string {
    _TitoloAnnuncioInserito = string;
    self.TitoloAnnuncio.text = string;
}

And with this you can change the delegate method to:

- (void)PassedData:(NSString *)text {   
    self.TitoloAnnuncioInserito = text;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top