如何在另一个类中生成的UIPopover中显示错误?

时间:2013-12-08 15:52:48

标签: objective-c ios6 uipopover

我有一个iPad应用程序(XCode5,ARC,iOS6和Storyboards)。在一个视图控制器中,我有一个允许用户登录或注册的弹出窗口。

点击“登录”或“注册”按钮会导致登录/注册处理在另一个类中进行。如果存在错误,则登录/注册例程会向调用方法返回描述错误的字符串。所有这一切都在弹出窗口仍然显示(如果没有错误,那么它被解雇)。

我的问题是:如何在popover中显示错误,该错误位于不同的类中? (我只是在底部的popover中显示错误字符串)

以下是在SettingsViewController.m中显示popover(其中一些已被删除以简洁起见)的代码:

//  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];
}

以下是执行登录/注册的代码(在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 个答案:

答案 0 :(得分:1)

您想要从popovercontroller显示错误。如果登录失败,为什么不从popover控制器类中显示“UIAlertView”?

例如,您可以显示如下警告消息框:

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