How to display an error in UIPopover generated in another class?

36 views Asked by At

I have an iPad app (XCode5, ARC, iOS6 and Storyboards). In one view controller I have a popover that allows the user to either login or register.

Tapping the Login or Register button causes the login/register processing to occur in another class. If there is an error, the login/register routines return a string describing the error to the invoking method. All this while the popover is still being displayed (if there were no errors, then it is dismissed).

My question is: how do I display the error in the popover, which is in a different class? (I was just going to display the error string in the popover at the bottom)

Here is the code to display the popover (some of it has been deleted for brevity) in SettingsViewController.m:

//  add Register button
UIButton *bRegister = [[UIButton alloc]initWithFrame:CGRectMake(55,200,140,30)];  //  add error msg: CGRectMake(55,250,140,30)
[bRegister setTitle:NSLocalizedString(@"Register",nil) forState:UIControlStateNormal];
[bRegister setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
bRegister.layer.borderWidth=1.0f;
bRegister.layer.borderColor=[[UIColor blueColor] CGColor];
CALayer *btnRegister = [bRegister layer];
[btnRegister setMasksToBounds:YES];
[btnRegister setCornerRadius:5.0f];
[popoverView addSubview:bRegister];

//  connect the buttons to their methods
[bLogin addTarget:self action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[bRegister addTarget:self action:@selector(registerButtonClicked) forControlEvents:UIControlEventTouchUpInside];

//  if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
    [popoverController dismissPopoverAnimated:YES];
}

//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:((UIButton *)oParseRegister).frame inView:self.view
                 permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];


//  handle login button tap
- (void) loginButtonClicked  {
ParseDotCom *pdc = [[ParseDotCom alloc]init];
NSString *errorReturned = [pdc loginPFUser:tfLoginID.text password:tfLoginPwd.text];
}

Here is the code that does the login/register (in ParseDotCom.m):

[PFUser logInWithUsernameInBackground: userID password: userPW
                     block:^(PFUser *user, NSError *error)  {
                     if(user) {
                          NSLog(@"\n\nuser logged on");
                          // Hooray! Let them use the app now.
                          }
                          else {
                              NSString *errorString = [error userInfo][@"error"];
                              NSLog(@"\n\nerror logging on: %@", errorString);
                             // Show the errorString and let the user try again.
                              }
                      }];

}

1

There are 1 answers

0
DerWOK On BEST ANSWER

You want to display an error from a popovercontroller. Why don't you just show an 'UIAlertView' from the popover controller class if login does not succeed?

For example, you may show an alert message box like so:

UIAlertView * alertBox = [[UIAlertView alloc] 
                                initWithTitle:@"Error"
                                      message:@"Wrong password"
                                     delegate:self
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:nil];
[alertBox show];