对此感到抱歉。 我想做的事: 在视图的文本字段中输入一个字符串(EnterCommandViewController)并单击“保存”按钮,这将关闭当前视图并返回另一个视图(DetectionViewController)并在当前视图(DetectionViewController)中显示UILabel中的字符串。所以我在EnterCommandViewController中定义了委托协议,我的问题是为什么用于检查某人是否正在侦听的respondToSelector不起作用。
我真的是iOS的初学者,我现在正在编写一个委托,将UITextField
形式的文本发送到UILabel
,但我发现respondToSelector
无法被调用使用NSLog
进行测试。
以下是我的参考代码:
EnterCommandViewController.h
#import <UIKit/UIKit.h>
#import "RscMgr.h"
@protocol EnterCommandDelegate <NSObject>
-(void) commandEntered:(NSString*)command;
@end
@interface EnterCommandViewController : UIViewController <RscMgrDelegate>
{
RscMgr* rscMgr;
__weak IBOutlet UITextField *inputTextField;
__unsafe_unretained id<EnterCommandDelegate> delegate;
}
-(void)sendMessage:(NSString*)message;
- (IBAction)cancelPressed;
- (IBAction)savePressed;
@property (nonatomic,assign)id delegate;
@end
EnterCommandViewController.m
#import "EnterCommandViewController.h"
@interface EnterCommandViewController () <UITextFieldDelegate>
{
@private
BOOL connected;
}
@end
@implementation EnterCommandViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
rscMgr = [[RscMgr alloc] init];
[rscMgr setDelegate:self];
// Do any additional setup after loading the view, typically from a nib.
inputTextField.text=@"";
[inputTextField becomeFirstResponder];
}
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
inputTextField.delegate = self;
}
-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
inputTextField.delegate = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cancelPressed {
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (IBAction)savePressed {
//is anyone listening
NSLog(@"the command is %@",inputTextField.text);
NSLog(@"Checking -- SomeMethod is listening");
if([delegate respondsToSelector:@selector(commandEntered:)]){
NSLog(@"SomeMethod is listening");
//send delegate function with the command entered by the user
[delegate commandEntered:inputTextField.text];
}
[self dismissViewControllerAnimated:YES completion:^{}];
}
DetectionViewController.h
#import <UIKit/UIKit.h>
#import "EnterCommandViewController.h"
@interface DetectionViewController : UIViewController <EnterCommandDelegate>{
__weak IBOutlet UILabel *showCommand;
}
- (IBAction)showSettings:(UIBarButtonItem *)sender;
@end
DetectionViewController.m
#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
@implementation DetectionViewController
- (IBAction)showSettings:(UIBarButtonItem *)sender {
}
-(void) viewDidLoad{
[super viewDidLoad];
showCommand.text=@"";
EnterCommandViewController* enterCVC = [[EnterCommandViewController alloc] init];
enterCVC.delegate = self;
}
#pragma mark - EnterCommandDelegate function(s)
-(void) commandEntered:(NSString *)command{
// showCommand.text = command;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"command: %@",command);
[self->showCommand setText:command];
});
}
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
答案 0 :(得分:0)
您没有正确设置delegate
。
EnterCommandViewController* enterCVC = [[EnterCommandViewController alloc] init];
enterCVC.delegate = self;
这不是在你的情况下设置delegate
的方法,因为你没有使用创建的enterCVC, instead a new instance is created from the storyboard when you are transitioning to
EnterCommandViewController实例(从你的评论明确表示你正在使用故事板为此)。
那么你可以做的就是delegate
来自prepareForSegue
中的DetectionViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get reference to the destination view controller
EnterCommandViewController* enterCVC = [segue destinationViewController];
enterCVC.delegate = self;
}