Ios 从另一个类调用自定义和委托方法

Ios 从另一个类调用自定义和委托方法,ios,objective-c,nsurlconnection,nsurlconnectiondelegate,Ios,Objective C,Nsurlconnection,Nsurlconnectiondelegate,我是目标c的初学者。我有两个类Connectivity和SignenViewController,我想要的是在另一个类的单个调用中调用一个类中定义的方法。我开始知道,我可以通过协议或授权来实现这一点,但仍然不知道是否有简单的方法来实现这一点。当我调试代码时,我可以看到在执行+(void)connectWithURL:(NSURL*)URL withData:(NSString*)postedData方法后,控件将转到SignenViewController。但是我想要的是首先执行connecti

我是目标c的初学者。我有两个类Connectivity和SignenViewController,我想要的是在另一个类的单个调用中调用一个类中定义的方法。我开始知道,我可以通过
协议
授权
来实现这一点,但仍然不知道是否有简单的方法来实现这一点。当我调试代码时,我可以看到在执行+
(void)connectWithURL:(NSURL*)URL withData:(NSString*)postedData
方法后,控件将转到
SignenViewController
。但是我想要的是首先执行connectivity类的所有方法(下面定义的委托方法),然后该控件应该返回到
SignenViewController
。希望,我问得很清楚

`//connectivity.h

 #import<Foundation/Foundation.h>
@interface Connectivity : NSObject<NSURLConnectionDelegate, NSURLConnectionDelegate>
+(void)connectWithURL:(NSURL *)URL withData:(NSString *)postedData;
@end 
//我正在调用该方法 (SignInViewController.m)

-(void)连接dFinishLoading:(NSURLConnection*)连接
{
NSString*response=[[NSString alloc]initWithData:receivedData编码:NSUTF8StringEncoding];
//NSLog(@“%@”,响应);
//NSLog(@“TAG==%d”,self.TAG);
NSDictionary*responseJson=[responsejsonvalue];
n错误*错误;
JSONDecoder*jsonKitDecoder=[JSONDecoder decoderWithParseOptions:JKParseOptionValidFlags];
NSDictionary*字典;
if([[JSONKITDecommer objectWithData:receivedData]valueForKey:@“d”])
{
dictionary=[[jsonKitDecoder objectWithData:receivedData]valueForKey:@“d”]objectFromJSONString];
}
其他的
{
dictionary=[NSJSONSerialization JSONObjectWithData:receivedData选项:kNilOptions | NSJSONReadingAllowFragments | NSJSONReadingMutableContainers错误:&错误];
}
//NSLog(@“responseJson==%@,[responseJson valueForKeyPath:@“data.WorkCell.label”]);
//NSLog(@“%@”,_代表);
如果(_代表){
if([_delegaterespondstoselector:@selector(requestFinished:withInfo:)])){
[_delegaterequestfinished:self with info:dictionary];
isRequest=假;
}
}
}
@协议请求代理
@可选的
-(void)requeststart:(Request*)请求;
-(void)requestFailed:(Request*)请求错误:(NSError*)错误;
@必需的
-(void)requestFinished:(Request*)requestwithinfo:(NSDictionary*)info;
@结束
这不是完美的答案,但您可以通过在Connectivity.m类中声明一个委托来匹配。在SignenViewController中实现委托的方法,通过这种方式可以实现这一点。
不要让get将委托分配给SignenView控制器。

TED重新编辑文本,并将不同的点从不同的行中分离出来。具体说明您所谈论的两个类。另外,请给出“我想要的是在另一个类的一次调用中调用一个类中定义的方法”的含义。关于这一点,请再次编辑问题并使其更清楚、更具体。我将在完成工作后重试。是的,我也希望如此,但实际发生的情况是,在执行[connection start]控制后,控制将转到SignenView控制器,并且再也不会回来执行NSURLConnection委派方法。
#import "Connectivity.h"
NSMutableData *_receivedData;
@implementation Connectivity
+(void)connectWithURL:(NSURL *)URL withData:(NSString *)postedData
{
//Create the Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
NSLog(@"posted url %@", URL);
//Create The Method "POST"
[request setHTTPMethod:@"POST"];
//header
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded"   forHTTPHeaderField:@"Content-Type"];

//Pass the String to the Server
NSString *postString = [NSString stringWithString:postedData];

//Check the Passed Value
NSLog(@"post string =%@", postedData);
//Convert the String to Data
NSData *data1 = [postedData dataUsingEncoding:NSUTF8StringEncoding];
//Apply the Data to the Body
[request setHTTPBody:data1];
//connection to the webserver

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];
[connection start];
}
#pragma mark NSURLConnection Delegate Methods

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

[_receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.

[_receivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data

NSLog(@"Succeeded! Received %lul bytes of data",[_receivedData length]);
NSString *responeString = [[NSString alloc] initWithData:_receivedData
                                                encoding:NSUTF8StringEncoding];

connection = nil;
_receivedData = nil;
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

connection = nil;
_receivedData = nil;

// inform the user

NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
@end
#import "SignInViewController.h"
#import "AlertMessageViewController.h"
#import "Connectivity.h"

@interface SignInViewController ()

@end

@implementation SignInViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)loginCustomUser
{

if([[self.emailTextField text] isEqualToString:@""] || [[self.passwordTextField text] isEqualToString:@""])
{
  [self presentViewController:[AlertMessageViewController alertWithTitle:@"Error" withMessage:@"Enter email and Password" preferredStyle:UIAlertControllerStyleAlert] animated:YES completion:nil];
}
else
{
NSString *data = [NSString stringWithFormat:@"{\"rqBody\":{\"emailId\":\"%@\",\"password\":\"%@\",\"userId\":\"%@\",\"idType\":\"%@\",\"firstName\":\"%@\",\"lastName\":\"%@\",\"contactNumber\":\"%@\",\"firstLineOfAddress\":\"%@\",\"localityName\":\"%@\",\"city\":\"%@\",\"state\":\"%@\",\"country\":\"%@\",\"roleType\":\"%@\",\"paymentProfile\":\"%@\",\"paymentDate\":\"%@\",\"registerationStatus\":\"%@\",\"loggedStatus\":\"%@\",\"lastLoggedInDate\":\"%@\",\"registerationDate\":\"%@\",\"profileStatus\":\"%@\"}}",[self.emailTextField text],[self.passwordTextField text],@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];

    NSURL *mainUrl= [NSURL URLWithString:@"http://192.168.1.5:8080/referamaid/app/noauth/signin"];

    [Connectivity connectWithURL:mainUrl withData:data];

}
}
@end`
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *response = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

//      NSLog(@"%@",response);
//    NSLog(@"TAG== %d",  self.tag);

    NSDictionary *responseJson = [response JSONValue];

    NSError *error;

    JSONDecoder *jsonKitDecoder = [JSONDecoder decoderWithParseOptions:JKParseOptionValidFlags];

    NSDictionary *dictionary;
    if ([[jsonKitDecoder objectWithData:receivedData] valueForKey:@"d"])
    {
        dictionary = [[[jsonKitDecoder objectWithData:receivedData] valueForKey:@"d"] objectFromJSONString];
    }
    else
    {
        dictionary = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions | NSJSONReadingAllowFragments |NSJSONReadingMutableContainers error:&error];
    }

    //      NSLog(@"responseJson== %@",  [responseJson valueForKeyPath:@"data.WorkCell.label"]);

//    NSLog(@"%@",_delegate);

        if(_delegate){
            if ([_delegate respondsToSelector:@selector(requestFinished:withInfo:)]) {

                [_delegate requestFinished:self withInfo:dictionary];
                isRequest = FALSE;
            }
        }
    }

@protocol RequestDelegate <NSObject>

@optional
-(void)requestStarted:(Request *)request;
-(void)requestFailed:(Request *)request withError:(NSError *)error;

@required
-(void)requestFinished:(Request *)request withInfo:(NSDictionary *)info;

@end