2013-09-01 104 views
4

我想從我的viewcontroller 2使用協議和委託推送一個字符串文本到viewcontroller 1。我對這種傳遞數據的方法很陌生,所以如果我以任何方式看起來無知,請原諒我。字符串顏色總是返回null。我會後我到目前爲止,代碼,如果有幫助,即時通訊使用的導航控制器,並使用導航後退按鈕從視圖控制器2的ViewController 1.使用協議和委託在視圖控制器之間傳遞數據

視圖控制器2

.H

@protocol PassString <NSObject> 
@required 
- (void) setSecondFavoriteColor:(NSString *)string; 
@end 
@interface ViewController2 : UIViewController{ 
UIButton *button; 
NSString *ee 
    id <PassString> delegate; 
    } 
    @property (retain) id delegate; 

的ViewController 2

的.m

@synthesize delegate; 

-(void)button{ 
ee = @"Blue Color"; 
[[self delegate] setSecondFavoriteColor:ee]; 

的ViewController 1.H

@interface ViewController1 : UIViewController <PassString>{ 
NSString*color; 
} 
@property (strong,nonatomic) NSString *color 

的ViewController 1.M

- (void)setSecondFavoriteColor:(NSString *)string 
{ 
color = string; 
NSLog(@"%@",color); 
} 
+1

我做了,如果我沒有,我甚至不會讓它成爲新的。我必須以某種方式使用protcol和代表在視圖控制器之間傳遞它。 –

+0

可能你忘了設置ViewController2.delegate = Viewcontroller1 – bhawesh

+0

爲什麼你必須這樣做「某種方式」?這是爲了上課嗎? –

回答

2

只是一對夫婦的事情,我在你的代碼注意到,你的財產應包含指定的協議:

@property (retain) id <PassString> delegate; 

而在在實現委託方法的類中的某些點,您必須將委託分配給視圖控制器1.例如:

[viewController2Instance setPassingDelegate:self]; 
+1

我忘了設置代表。謝謝 –

相關問題