Objective c 如何在目标C中正确分配方法

Objective c 如何在目标C中正确分配方法,objective-c,methods,uiviewcontroller,Objective C,Methods,Uiviewcontroller,我开始使用iPhone和iPad应用程序,但仍然不知道如何做一些事情 我正在开发一个有登录屏幕的应用程序。当我用另一种应用程序语言开发时,我有一个名为WebService的类,在这个类中,我完成了与Web服务通信并返回输出或布尔值的所有工作 但是在目标C中,我不知道怎么做 这是我的密码: ViewController.m - (IBAction)login:(id)sender { /* Verify if username and password are correct */

我开始使用iPhone和iPad应用程序,但仍然不知道如何做一些事情

我正在开发一个有登录屏幕的应用程序。当我用另一种应用程序语言开发时,我有一个名为
WebService
的类,在这个类中,我完成了与Web服务通信并返回输出或布尔值的所有工作

但是在目标C中,我不知道怎么做

这是我的密码:

ViewController.m

- (IBAction)login:(id)sender {
    /* Verify if username and password are correct */
    Boolean loginCorrecto = [[WebService alloc] login:self.textFieldUsername.text password:self.textFieldPassword.text];

    /* If login is correct, go to another view */
    if (loginCorrecto) {
        CupsViewController *cupsView = [self.storyboard instantiateViewControllerWithIdentifier:@"cupsViewController"];
        [self.navigationController pushViewController:cupsView animated:YES];
    } 
    /* If not, show an error message */
    else {

    }
}
- (Boolean)login:(NSString *)username password:(NSString *)password
{
    /* HTTP POST */
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:URL_CUPS]];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [NSString stringWithFormat: @"username=%@&password=%@", username, password];

    [request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
    NSDictionary* jsonArray=[NSJSONSerialization
                    JSONObjectWithData:theData
                    options:0
                    error:nil];

    //Get values of response of web service
    status = [jsonArray objectForKey:@"status"];
    message = [jsonArray objectForKey:@"message"];

    if([status isEqualToString:@"Ok"]) {
        id results = [jsonArray objectForKey:@"results"];

        if(results != [NSNull null]) {
            for(int r = 0; r < [results count]; r++) {
            //Get more values
            }
        }
    }
    else {
        //Show error
    }
}
WebService.m

- (IBAction)login:(id)sender {
    /* Verify if username and password are correct */
    Boolean loginCorrecto = [[WebService alloc] login:self.textFieldUsername.text password:self.textFieldPassword.text];

    /* If login is correct, go to another view */
    if (loginCorrecto) {
        CupsViewController *cupsView = [self.storyboard instantiateViewControllerWithIdentifier:@"cupsViewController"];
        [self.navigationController pushViewController:cupsView animated:YES];
    } 
    /* If not, show an error message */
    else {

    }
}
- (Boolean)login:(NSString *)username password:(NSString *)password
{
    /* HTTP POST */
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:URL_CUPS]];

    [request setHTTPMethod:@"POST"];

    NSString *postString = [NSString stringWithFormat: @"username=%@&password=%@", username, password];

    [request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
    NSDictionary* jsonArray=[NSJSONSerialization
                    JSONObjectWithData:theData
                    options:0
                    error:nil];

    //Get values of response of web service
    status = [jsonArray objectForKey:@"status"];
    message = [jsonArray objectForKey:@"message"];

    if([status isEqualToString:@"Ok"]) {
        id results = [jsonArray objectForKey:@"results"];

        if(results != [NSNull null]) {
            for(int r = 0; r < [results count]; r++) {
            //Get more values
            }
        }
    }
    else {
        //Show error
    }
}
-(布尔)登录:(NSString*)用户名密码:(NSString*)密码
{
/*HTTP POST*/
NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:URL_CUPS]];
[请求设置HttpMethod:@“POST”];
NSString*postString=[NSString stringWithFormat:@“用户名=%@&密码=%@”,用户名,密码];
[request setValue:[NSString stringWithFormat:@“%d”,[postString长度]]用于HttpHeaderField:@“内容长度”];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding];
[[NSURLConnection alloc]initWithRequest:请求委托:self];
}
-(void)连接:(NSURLConnection*)连接未接收数据:(NSData*)数据{
NSDictionary*jsonArray=[NSJSONSerialization
JSONObjectWithData:数据
选项:0
错误:无];
//获取web服务响应的值
状态=[jsonArray objectForKey:@“状态”];
message=[jsonArray objectForKey:@“message”];
如果([状态为IsequalString:@“Ok”]){
id results=[jsonArray objectForKey:@“results”];
如果(结果!=[NSNull]){
对于(int r=0;r<[结果计数];r++){
//获取更多值
}
}
}
否则{
//显示错误
}
}
目标


所以我想要的是在
WebService.m
的登录方法中,如果
status
等于“Ok”,则返回true。但当该方法完成时,状态仍然是
nil
。我该怎么办?在
ViewController.m
中,我拥有或必须使用类来执行
httppost

[[NSURLConnection alloc]initWithRequest:request delegate:self]将触发异步连接。您应该声明一个协议(相当于java中的接口)并在ViewController中实现它。异步方法完成后,调用协议中的一种方法并传递所需的任何数据。Objective-C中的设计与您更熟悉的任何其他语言都不应该有任何不同

除了少数例外,网络上的交互应该是异步的,以避免阻塞UI。您已经在控制器中实现了异步
NSURLConnection
API。您的部分问题可能与哪个类应该调用web服务有关。通常,我避免让视图控制器直接进行这些调用。它会导致视图控制器类膨胀。相反,就像您的
WebService
服务类,无论您使用何种语言开发,我都会在那里处理此类调用。然后,您需要一种向控制器报告响应状态的方法。正如另一位回答者所建议的,您可以声明一个
WebServiceDelegate
协议,并在控制器中实现该协议。因此,您的
WebService
实例充当您为与web服务交互而创建的
NSURLConnection
实例的委托,视图控制器充当它生成的
WebService
实例的委托。一切都保持异步;并且避免让视图控制器直接负责处理web服务。也就是说,一些开发人员并不反对直接在视图控制器实现中实现web服务调用

或者,您可以在
WebService
类上声明一个完成块,并在
NSURLConnection
完成时执行该块,例如


最后,您在发布的代码中对
nsurlconnectionelegate
协议的实现没有考虑到
connection:didReceiveData:
可能会被多次调用,其中包含必须汇编的数据块。最好创建一个
NSMutableData
ivar,在每次调用
connection:didReceiveData:
时向其追加数据。最后,您可以在
connectiondFinishLoading
中进行处理。(并实现
connection:didfailwitheror:

与您无关的问题,如果您刚刚开始新的开发,请尝试
AFNetworking
。处理API请求既简单又容易。我不知道我是否理解你。我必须创建一个协议(命名为PX),我的ViewController必须实现PX。PX有
login方法
,其中包含my
WebService.m
和另一个方法(my),该方法将查找登录方法的结果,在我的例子中是变量
status
。它是?