0

I have several ViewControllers ,and a "networkClient" subclass of NSObject which offer functions of get/post data.

if "networkClient" occur an error when get/post data,the functions will return an error info for the ViewControllers who call get/post functions.

I found I always have lots of same code in different ViewControllers, such as:

- (void)showAlertWithString:(NSString *)string{
    SIAlertView *alertView = [[SIAlertView alloc]initWithTitle:@"Error" andMessage:string];
    [alertView addButtonWithTitle:@"OK" 
    type:SIAlertViewButtonTypeDestructive handler:^(SIAlertView *alertView){
    }];
    [alertView show];
}

since I don't want to include the 3rd party library "SIAlertView" and write the code -(void)showAlertViewWithString in every ViewControllers.

I'm wondering is there a way to include the library and write the code only in "networkClient" and return this -(void)showAlertViewWithString to the ViewControllers and let them execute it in the ViewControllers when the error happened?


update: thanks for the answers, as I'm a beginner of iOS development I just know there's a method called 'block' and after I tried, it seemed work.

below is the code which I tried to use block instead delegate to call "show alert" in ViewControllers.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"action" : @"create_user",
                             @"email" : email,
                             @"password" :password,
                             @"user_name" :name,
                             @"mobile": number
                             };
[manager POST:kLoginURL
   parameters:parameters
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          SIAlertView *alertView = [[SIAlertView alloc]initWithTitle:@"Alert" andMessage:@"Success"];
          [alertView addButtonWithTitle:@"ok" type:SIAlertViewButtonTypeDestructive handler:^(SIAlertView *alertView){

          }];
          [alertView show];
      }
//              [self.delegate createAccountResult:(responseObject)];
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];
}
2
  • Try to subclass the UIViewController with your networkClient in it so you can handle the success/error blocks. Commented Feb 10, 2015 at 1:57
  • I have read some articles about using a block, I found that I could just write the function I want to execute in the block and it will execute in the ViewControllers which call the "networkClient"method. Commented Feb 10, 2015 at 2:50

1 Answer 1

1

So create a custom subclass of UIViewController, and implement that method in the class. Then make all your view controllers inherit from your custom class.

Alternately you could create a category of UIViewController.

Sign up to request clarification or add additional context in comments.

2 Comments

yes~! that's a good idea I used to try this, it is very useful when several ViewControllers use same functions. I also heard about "block" maybe it is a better choice when I have already created the ViewControllers....>_<.... I should learn about how to use block...
Blocks are very useful, but I'm not sure how they would apply here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.