我想從我的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);
}
我做了,如果我沒有,我甚至不會讓它成爲新的。我必須以某種方式使用protcol和代表在視圖控制器之間傳遞它。 –
可能你忘了設置ViewController2.delegate = Viewcontroller1 – bhawesh
爲什麼你必須這樣做「某種方式」?這是爲了上課嗎? –