2013-12-13 66 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; 
}