未为协议调用委托 respondsToSelector
Delegate respondsToSelector not being called for Protocol
我是委托方法的新手。我想知道为什么我的 "respondsToSelector" 没有被要求代表我。我确信我把所有东西都放在了正确的地方。如果您需要更多信息,请告诉我!!!我环顾四周,但仍未找到有效的解决方案!
谢谢大家!
MODAL.h
@protocol Profession <NSObject>
-(void)profession:(NSString *)job;
@end
@interface ProfessionListingViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>{
NSString *string;
}
@property (nonatomic, weak) id<Profession> myDelegate;
@end
MODAL.m
@synthesize myDelegate;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.profession = @"Testing!!";
if([self.myDelegate respondsToSelector:@selector(profession:)]){
[self.myDelegate profession:self.profession];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
VIEW.h
#import <UIKit/UIKit.h>
#import "ProfessionListingViewController.h"
#import "LocationViewController.h"
@interface ProfessionViewController : UIViewController <Profession>
VIEW.m
-(IBAction)toProfessionListView:(id)sender{
ProfessionListingViewController *modal = [[ProfessionListingViewController alloc] init];
modal.myDelegate = self;
[self performSegueWithIdentifier:@"ModalProfessionList" sender:sender];
}
-(void)profession:(NSString *)job{
NSString *test = job;
NSLog(@"%@", test);
}
在我看来,您实际上是在倒退 - 我会声明委托视图,模态应成为视图的委托。
就是说,如果您想这样做,我怀疑问题是您正在对模态视图控制器的新实例执行转接:
[self performSegueWithIdentifier:@"ModalProfessionList" sender:sender];
您可能想要做的是使用 prepareForSegue:
,获取模态视图的实例,并在那里设置委托:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ModalProfessionList"])
{
ProfessionListingViewController *modal = [segue destinationViewController];
modal.myDelegate = self;
}
}
我是委托方法的新手。我想知道为什么我的 "respondsToSelector" 没有被要求代表我。我确信我把所有东西都放在了正确的地方。如果您需要更多信息,请告诉我!!!我环顾四周,但仍未找到有效的解决方案!
谢谢大家!
MODAL.h
@protocol Profession <NSObject>
-(void)profession:(NSString *)job;
@end
@interface ProfessionListingViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>{
NSString *string;
}
@property (nonatomic, weak) id<Profession> myDelegate;
@end
MODAL.m
@synthesize myDelegate;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.profession = @"Testing!!";
if([self.myDelegate respondsToSelector:@selector(profession:)]){
[self.myDelegate profession:self.profession];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
VIEW.h
#import <UIKit/UIKit.h>
#import "ProfessionListingViewController.h"
#import "LocationViewController.h"
@interface ProfessionViewController : UIViewController <Profession>
VIEW.m
-(IBAction)toProfessionListView:(id)sender{
ProfessionListingViewController *modal = [[ProfessionListingViewController alloc] init];
modal.myDelegate = self;
[self performSegueWithIdentifier:@"ModalProfessionList" sender:sender];
}
-(void)profession:(NSString *)job{
NSString *test = job;
NSLog(@"%@", test);
}
在我看来,您实际上是在倒退 - 我会声明委托视图,模态应成为视图的委托。
就是说,如果您想这样做,我怀疑问题是您正在对模态视图控制器的新实例执行转接:
[self performSegueWithIdentifier:@"ModalProfessionList" sender:sender];
您可能想要做的是使用 prepareForSegue:
,获取模态视图的实例,并在那里设置委托:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ModalProfessionList"])
{
ProfessionListingViewController *modal = [segue destinationViewController];
modal.myDelegate = self;
}
}