objective-c - 如何使自定义 UIView 目标 c 的自定义委托,确定按钮不起作用
问题描述
这是我受影响的代码:
#import "AddTeamView.h"
#import <AFNetworking.h>
@implementation AddTeamView
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self=[super initWithCoder:aDecoder];
if (self)
{
[self customInit];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self)
{
[self customInit];
}
return self;
}
-(void)customInit
{
[[NSBundle mainBundle]loadNibNamed:@"AddTeamView" owner:self options:nil];
[self addSubview:self.contentView];
}
- (IBAction)okButton:(UIButton *)sender
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"team_name":self.enterNameTextField.text
};
[manager POST:@"https://api.cartolafc.globo.com/times?q=team_name" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
}progress:nil success:^(NSURLSessionTask *task, id responseObject) {
// [self stopHud];
NSLog(@"JSON: %@", responseObject);
NSDictionary *response = (NSDictionary *)responseObject;
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSInteger statusCode = error.code;
NSLog(@"%ld",(long)statusCode);
// [self stopHud];
}];
}
我创建一个.Xib
文件,如:
当我按下Ok
按钮时,没有响应,也没有任何反应。现在我如何Delegate
为自定义声明自定义UIView
以及为什么这里需要委托?谁能解释一下我能做些什么来获得 API 的响应?
解决方案
打开您的 AddTeamView.h 和此代码。
@protocol AddTeamDelegate <NSObject>
- (IBAction)okButton:(UIButton *)sender;
@end
@property id <AddTeamDelegate> delegate;
并在 AddTeamView.m 文件中合成该属性,如下所示。
@synthesize delegate;
添加此代码在您将 AddTeamView 添加为子视图的 viewController.h 文件中。
@interface ViewController : UIViewController <AddTeamDelegate>
在您的 viewController.m 文件中添加此代码,同时将 AddTeamView 添加为子视图。
AddTeamView.delegate = self;
并在同一文件中为确定按钮添加此方法
- (IBAction)okButton:(UIButton *)sender
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *params = @{@"team_name":self.enterNameTextField.text
};
[manager POST:@"https://api.cartolafc.globo.com/times?q=team_name" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
}progress:nil success:^(NSURLSessionTask *task, id responseObject) {
// [self stopHud];
NSLog(@"JSON: %@", responseObject);
NSDictionary *response = (NSDictionary *)responseObject;
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSInteger statusCode = error.code;
NSLog(@"%ld",(long)statusCode);
// [self stopHud];
}];
}
推荐阅读
- javascript - 为什么 javascript 不替换所有模式实例?
- html - 图像上的透明垫手风琴
- python - 将 namedtuples 与现有列表一起使用
- java - Spring Webflux WebFilterChain中的URL模式过滤
- javascript - 如何将数组从内容脚本发送到弹出脚本
- c++ - 将具有公共基类的联合成员的虚函数作为另一个成员调用是未定义的行为吗?
- python-3.x - 如何计算二进制图像中 blob 的实例(白色 blob 和黑色作为背景颜色)
- azure - 如何从 Kusto.Explorer 查询我的 Application Insights 日志(分析)?
- python - 快速将奇怪的门控应用于 pandas 数据
- php - PHP Mail - 有时会发送空白电子邮件