2014-02-26 26 views
0

我知道有很多關於在不同視圖控制器之間傳遞消息的問題。我已經檢查過他們,但我無法工作。簡單的代表示例iOS不工作

我已經按照這個教程:http://www.youtube.com/watch?v=XZWT0IV8FrI與導航控制器更換故事板,但我遇到了以下問題一遍遍運行:「無法找到...協議聲明」

下面是代碼:

FirstViewController.h

#import "SecondViewController.h" 

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{ 
    //In this line above is where I get the error 'Cannot find protocol declaration for SecondViewControllerDelegate' 
    IBOutlet UITextField *userNameTextField; 
} 

@property (nonatomic, strong) UITextField *userNameTextField; 

-(IBAction)goNext:(id)sender; 

@end 

FirstViewController.m

#import "FirstViewController.h" 

@interface FirstViewController() 

@end 

@implementation FirstViewController 

@synthesize userNameTextField; 


-(void)goNext:(id)sender{ 

    SecondViewController *secondVC = [[SecondViewController alloc]init]; 
    secondVC.delegate = self; 
    [self.navigationController pushViewController:secondVC animated:YES]; 
} 

-(void)done:(NSString*)name{ 

    NSLog(@"BACK in firstVC"); 
    userNameTextField.text = name; 
} 

@end 

SecondViewController.h

#import "FirstViewController.h" 

@protocol SecondViewControllerDelegate <NSObject> 

-(void)done:(NSString*)someText; 

@end 

@interface SecondViewController : UIViewController{ 

    IBOutlet UITextField *someText; 
    IBOutlet UIButton *returnButton; 
    id delegate; 
} 

@property (assign, nonatomic) id <SecondViewControllerDelegate> delegate; 
@property (strong, nonatomic) UITextField *someText; 

-(IBAction)goBack:(id)sender; 

@end 

SecondViewController.m

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

@synthesize someText; 
@synthesize delegate = _delegate; 

-(void)goBack:(id)sender{ 

    [self.delegate done:someText.text]; 

    FirstViewController *firstVC = [[FirstViewController alloc]init]; 

    [self.navigationController pushViewController:firstVC animated:YES]; 
} 

@end 

回答

2

在你SecondViewControllergoBack實現您要創建一個新的FirstViewController而不是彈出你的導航控制器,代碼應該讀...

-(void)goBack:(id)sender{ 

    [self.delegate done:someText.text]; 

    [self.navigationController popViewControllerAnimated:YES]; 

} 

而且也在你SecondViewController.h卸妝此#進口「 FirstViewController.h「,因爲它不再需要,可能會讓編譯器感到困惑

+0

好的,完成了。但它不能解決我與代表的問題。 – guardabrazo

+0

在你的'SecondViewController.h'去除這個'#import「FirstViewController.h」',因爲它不再需要,可能會令編譯器 – Flexicoder

+0

工作!你會用這個答案來回答,所以我可以將其標記爲已接受? – guardabrazo

2

你的協議名稱是EYSSecondViewControllerDelegate:

@protocol EYSSecondViewControllerDelegate <NSObject> 

但你怎麼稱呼它SecondViewControllerDelegate在兩個地方:

@interface EYSFirstViewController : UIViewController <SecondViewControllerDelegate>{... 
@property (assign, nonatomic) id <SecondViewControllerDelegate> delegate;... 

確保名稱匹配,它應該工作正常。

+0

對不起,我刪除了我的類前綴以增加一些清晰度。現在編輯。 – guardabrazo

+0

@guardabrazo,可能是一個更好的主意來粘貼你的實際代碼... – Wain

+0

你在哪個iOS?嘗試刪除ID委託;和@synthesize委託= _delegate;它應該有所幫助。 – Greg

0

SecondViewController.h

刪除線id delegate;

SecondViewController.m

更新代碼 - >[delegate done:someText.text];
'自我'。刪除 試試吧

+0

完成,但我仍然有同樣的問題。 – guardabrazo