我希望我正在工作的程序的根视图控制器是PFQueryTableViewController
,但是,我需要该应用程序确保用户在PFQueryTableViewController
尝试查询之前登录。
我尝试创建一个RootTabBarViewController
,其初始视图是PFQueryTableViewController
,并在RootTabBarViewController
的。h和。m文件中放入以下内容:
//
// RootTabBarViewController.h
//
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface RootTabBarViewController : UITabBarController <PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate>
@end
//
// RootTabBarViewController.m
//
#import "RootTabBarViewController.h"
#import "PlannrLogInViewController.h"
@interface RootTabBarViewController ()
@end
@implementation RootTabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (![PFUser currentUser]) { // No user logged in
// Create the log in view controller
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self]; // Set ourselves as the delegate
// Create the sign up view controller
PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
[signUpViewController setDelegate:self]; // Set ourselves as the delegate
// Assign our sign up controller to be displayed from the login controller
[logInViewController setSignUpController:signUpViewController];
// Present the log in view controller
[self presentViewController:logInViewController animated:YES completion:NULL];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - PFLoginViewController Delegate
// Sent to the delegate to determine whether the log in request should be submitted to the server.
- (BOOL)logInViewController:(PFLogInViewController *)logInController shouldBeginLogInWithUsername:(NSString *)username password:(NSString *)password {
// Check if both fields are completed
if (username && password && username.length != 0 && password.length != 0) {
return YES; // Begin login process
}
[[[UIAlertView alloc] initWithTitle:@"Missing Information"
message:@"Make sure you fill out all of the information!"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
return NO; // Interrupt login process
}
// Sent to the delegate when a PFUser is logged in.
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissViewControllerAnimated:YES completion:NULL];
}
// Sent to the delegate when the log in attempt fails.
- (void)logInViewController:(PFLogInViewController *)logInController didFailToLogInWithError:(NSError *)error {
NSLog(@"Failed to log in...");
}
// Sent to the delegate when the log in screen is dismissed.
- (void)logInViewControllerDidCancelLogIn:(PFLogInViewController *)logInController {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - PFSignUpViewController Delegate
// Sent to the delegate to determine whether the sign up request should be submitted to the server.
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info {
BOOL informationComplete = YES;
// loop through all of the submitted data
for (id key in info) {
NSString *field = [info objectForKey:key];
if (!field || field.length == 0) { // check completion
informationComplete = NO;
break;
}
}
// Display an alert if a field wasn't completed
if (!informationComplete) {
[[[UIAlertView alloc] initWithTitle:@"Missing Information"
message:@"Make sure you fill out all of the information!"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
}
return informationComplete;
}
// Sent to the delegate when a PFUser is signed up.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didSignUpUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES]; // Dismiss the PFSignUpViewController
}
// Sent to the delegate when the sign up attempt fails.
- (void)signUpViewController:(PFSignUpViewController *)signUpController didFailToSignUpWithError:(NSError *)error {
NSLog(@"Failed to sign up...");
}
// Sent to the delegate when the sign up screen is dismissed.
- (void)signUpViewControllerDidCancelSignUp:(PFSignUpViewController *)signUpController {
NSLog(@"User dismissed the signUpViewController");
}
@end
这是我的。m文件为PFQueryTableViewController子类:
//
// EventPFQueryTableViewController.m
//
#import "EventPFQueryTableViewController.h"
#import "Event.h"
@interface EventPFQueryTableViewController ()
@end
@implementation EventPFQueryTableViewController
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self)
{
self.parseClassName = kEventListClassKey;
}
return self;
}
- (PFQuery *)queryForTable
{
PFQuery *query = [Event query];
[query whereKey:kEventListFieldKeyUser equalTo:[PFUser currentUser]];
return query;
}
/*
@end
然而,这个选项卡栏视图控制器的viewDidLoad
函数中的代码似乎没有在PFQueryTableViewController
(根栏视图控制器的第一个视图)加载并尝试查询之前运行,因为我仍然得到异常:
2014-07-10 20:46:04.226 Plannr[12810:60b] *终止应用程序由于未捕获异常'NSInvalidArgumentException',原因:'不能做比较查询类型:(null)'
我如何确保登录视图加载远在EventPFQueryTableViewController
之前?
首先,你可以通过像这样修改EventPFQueryTableViewController的- (PFQuery *)queryForTable
方法来防止应用程序崩溃:
if ([PFUser currentUser]) { //User logged in.
PFQuery *query = [Event query];
[query whereKey:kEventListFieldKeyUser equalTo:[PFUser currentUser]];
return query;
}
else {
[super objectsDidLoad:error]; //Return failed to turn off spinner.
return nil;
}
你还想在你的PFQueryTableViewController中为登录事件设置一个通知监听器。这个会在用户登录更新表视图时调用比如handleLogIn:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleLogIn:) name:kLoggedInEvent object:nil];
在你的PFQueryTableViewController中添加handleLogIn方法:
- (void)handleLogIn:(NSNotification*) notification {
[self loadObjects]
}
然后当注册/登录成功时,调用你创建的通知使TableView得到更新:
[[NSNotificationCenter defaultCenter] postNotificationName:kLoggedInEvent object:nil userInfo:nil];