繁体   English   中英

ios协议无法正常运行

[英]ios protocol not functioning properly

//AddSideBarProtocol.h
@protocol AddSideBarProtocol  <NSObject>

- (IBAction)barButtonTapped:(id)sender;

@end

我正在创建上述协议以在我的所有视图控制器中使用。 我对该协议的实现如下:

//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];

}

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

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

id <AddSideBarProtocol> addSide;
addSide = addVehicle;

[addSide barButtonTapped:sender];

但是该协议无法正常运行,所以我缺少什么?

苹果报价:

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

运作方式:

1)声明协议(就像您所做的那样)2)在接口中添加它们实现此协议的ex:

//AddVehicleViewController.h
@interface AddVehicleViewController : UIViewController <AddSideBarProtocol>

这意味着此类应实现协议中声明的方法。 如果您未实现在每个这些类中都未声明为可选的所有方法,则会收到警告。

3)您可以在代码中使用它:ex:

AddVehicleViewController *addVehicle = [[AddVehicleViewController alloc] init];
[addVehicle barButtonTapped:sender];

关于协议的一些阅读: Apple Doc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM