【问题标题】:Simple delegate example for iOS not workingiOS 的简单委托示例不起作用
【发布时间】:2014-02-26 11:31:49
【问题描述】:

我知道有很多关于在不同视图控制器之间传递消息的问题。我都检查过了,但我无法正常工作。

我已按照本教程进行操作: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

【问题讨论】:

    标签: ios objective-c uiviewcontroller delegates protocols


    【解决方案1】:

    在您的 SecondViewController goBack 实现中,您正在创建一个新的 FirstViewController 而不是弹出您的导航控制器,代码应为...

    -(void)goBack:(id)sender{
    
        [self.delegate done:someText.text];
    
        [self.navigationController popViewControllerAnimated:YES];
    
    }
    

    在你的 SecondViewController.h 卸妆器中,这个 #import "FirstViewController.h" 不再需要,并且可能会混淆编译器

    【讨论】:

    • 在你的 SecondViewController.h 卸妆器中这个 #import "FirstViewController.h" 因为它不再需要并且可能会混淆编译器
    • 我将更新此答案以包含该事实,因为您需要同时执行这两个操作,即删除 .h 并弹出到视图控制器
    【解决方案2】:

    您的协议名称是 EYSSecondViewControllerDelegate:

    @protocol EYSSecondViewControllerDelegate <NSObject>
    

    但你在两个地方称它为 SecondViewControllerDelegate:

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

    确保名称匹配并且它应该可以正常工作。

    【讨论】:

    • 抱歉,我删除了我的类前缀以增加清晰度。现在已编辑。
    • 你在哪个 iOS 上?尝试删除 id 委托;和 @synthesize 委托 = _delegate;它应该有帮助。
    • @Greg iOS7。我已经按照你的建议做了,但我仍然遇到同样的问题。
    • 尝试清除您的项目,重新启动 Xcode 并再次运行。
    【解决方案3】:

    SecondViewController.h

    删除行id delegate;

    SecondViewController.m

    更新代码 -> [delegate done:someText.text];
    'self.' 移除 试试看

    【讨论】:

    • 完成了,但我仍然有同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2015-11-23
    • 2016-03-02
    • 1970-01-01
    相关资源
    最近更新 更多