ios协议无法正常运行

ios协议无法正常运行,ios,objective-c,xcode,protocols,Ios,Objective C,Xcode,Protocols,我使用以下代码行从另一个名为“MaintenanceViewControlle”的视图控制器调用协议: //AddVehicleViewController.m - (IBAction)barButtonTapped:(id)sender{ [self.view endEditing:YES]; [lblToolBarTitle setText:@"Vehicle Management"]; tblViewSideBar = [[UITableView alloc]initWithFrame:

我使用以下代码行从另一个名为“MaintenanceViewControlle”的视图控制器调用协议:

//AddVehicleViewController.m
- (IBAction)barButtonTapped:(id)sender{
[self.view endEditing:YES];
[lblToolBarTitle setText:@"Vehicle Management"];

tblViewSideBar = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, self.view.frame.size.height-44)];
tblViewSideBar.delegate = self;
tblViewSideBar.dataSource = self;
tblViewSideBar.separatorStyle = UITableViewCellSeparatorStyleNone;
[tblViewSideBar setBackgroundColor:[UIColor lightGrayColor]];

btnToClose = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[btnToClose setBackgroundColor:[UIColor clearColor]];
[btnToClose addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

addSideBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 200, self.view.frame.size.height)];
[addSideBarView setBackgroundColor:[UIColor blackColor]];

addSideBarView.frame = CGRectMake(-200, 44, 200, self.view.frame.size.height);

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     addSideBarView.frame = CGRectMake(0, 44, 200, self.view.frame.size.height);
                 }
                 completion:nil];

[self.view addSubview:btnToClose];
[self.view addSubview:addSideBarView];
[addSideBarView addSubview:tblViewSideBar];

}
AddVehicleViewController*addVehicle=[[AddVehicleViewController alloc]init];
id addSide;
addSide=addVehicle;
[addSide BarbuttonApped:发送方];
但是这个协议不能正常运行,所以我缺少什么呢???

引用苹果的话:

类接口声明与该类关联的方法和属性。相反,协议用于声明独立于任何特定类的方法和属性

工作原理:

1) 您声明了您的协议(就像您所做的那样) 2) 您可以在接口中添加实现此协议的接口 例:

一些关于协议的阅读:

您得到的错误是什么?我没有得到任何错误,但此方法用于在点击按钮时添加视图,该视图不是使用协议添加的。为什么要将addVehicle强制转换为addSide?为什么不执行[addVehicle barButtonTapped:sender]?如果您已正确地将AddVehicleViewController.h放入,则不应出现错误/warnings@KIDdAe::如果我使用您的解决方案,那么声明协议的含义是什么,我可以在不声明协议的情况下调用该方法。我已经尝试过你的解决方案,但它不起作用,正如我在上面的评论中提到的一样,同样的问题也会发生。只是铸造不会改变任何事情。。协议函数(barButtonTapped:)应该在您想要调用barButtonTapped函数的每个类中实现
barButtonTapped:
?您的方法中有许多视图,哪一个没有出现?是的,该方法被正确调用和执行,并且没有一个视图出现。此方法应由可视控制器调用(
[self.view addsubview:aView]
没有其他含义)。在您的上一个代码片段中,您似乎在显示控制器之前调用了您的方法。我不明白您的意思,请您解释一下如何执行?我的意思是在这一行
AddVehicleViewController*addVehicle=[[AddVehicleViewController alloc]init]添加车辆的视图未添加到当前视图中,然后添加类似于
[self.view addSubview:btnToClose]的行无效(因为self是addVehicle且不显示)。我不知道您想要实现什么,但似乎
MaintenanceViewController
应该是必须实现协议并调用方法的人。(右边显示的是他吗?)
//AddVehicleViewController.m
- (IBAction)barButtonTapped:(id)sender{
[self.view endEditing:YES];
[lblToolBarTitle setText:@"Vehicle Management"];

tblViewSideBar = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, self.view.frame.size.height-44)];
tblViewSideBar.delegate = self;
tblViewSideBar.dataSource = self;
tblViewSideBar.separatorStyle = UITableViewCellSeparatorStyleNone;
[tblViewSideBar setBackgroundColor:[UIColor lightGrayColor]];

btnToClose = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[btnToClose setBackgroundColor:[UIColor clearColor]];
[btnToClose addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

addSideBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 200, self.view.frame.size.height)];
[addSideBarView setBackgroundColor:[UIColor blackColor]];

addSideBarView.frame = CGRectMake(-200, 44, 200, self.view.frame.size.height);

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^ {
                     addSideBarView.frame = CGRectMake(0, 44, 200, self.view.frame.size.height);
                 }
                 completion:nil];

[self.view addSubview:btnToClose];
[self.view addSubview:addSideBarView];
[addSideBarView addSubview:tblViewSideBar];

}
AddVehicleViewController *addVehicle = [[AddVehicleViewController alloc] init];

id <AddSideBarProtocol> addSide;
addSide = addVehicle;

[addSide barButtonTapped:sender];
//AddVehicleViewController.h
@interface AddVehicleViewController : UIViewController <AddSideBarProtocol>
AddVehicleViewController *addVehicle = [[AddVehicleViewController alloc] init];
[addVehicle barButtonTapped:sender];