2013-12-13 68 views
0

我有两个视图控制器...传递数据TextView使用协议

ViewController 1有一个标签,应该显示通过TextView输入的文本。

的视图控制器2具有通过文本的视图控制器1

标签要做到这一点我使用的协议这样一个TextView

头文件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 

在执行文件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 

在视图控制器1,就像我说的,这是一个UILabel应该显示在视图控制器2

实现文件VC1.m

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

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

的TextView的在输入的文本头文件VC1.h实现这个属性:

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

我的问题是,我不明白为什么我的标签不显示文本..它仍然是空的...我不能从1 ViewController中的ViewController 2传递数据 你能帮忙吗?

回答

0

撇开与初始小写字母命名变量的问题...

您设置的委托方法的字符串值,不过,使用iOS是不是OS X,有没有绑定,所以才更改属性的值不会自动更改文本字段中显示的值。

你有几个选择,一个是更新委托方法中的显示。

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

另一种方法是在属性值发生变化时设置显示在标签中 - 这更健壮一点。

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

而与此,您可以更改的委托方法:

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