首页 > 解决方案 > 如何使自定义 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 的响应?

标签: objective-c

解决方案


打开您的 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];
}];
}

推荐阅读