我知道有很多關於在不同視圖控制器之間傳遞消息的問題。我已經檢查過他們,但我無法工作。簡單的代表示例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
好的,完成了。但它不能解決我與代表的問題。 – guardabrazo
在你的'SecondViewController.h'去除這個'#import「FirstViewController.h」',因爲它不再需要,可能會令編譯器 – Flexicoder
工作!你會用這個答案來回答,所以我可以將其標記爲已接受? – guardabrazo