1

私が取り組んでいるプログラムのルート ビュー コントローラーを にしたいのですが、クエリを試行するPFQueryTableViewController前にユーザーがログインしていることを確認するアプリが必要です。PFQueryTableViewController

RootTabBarViewController初期ビューが である を作成し、の .h および .m ファイルPFQueryTableViewControllerに以下を入れてみました。RootTabBarViewController

//
//  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

PFQueryTableViewController サブクラスの .m ファイルは次のとおりです。

//
//  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ログインビューが 前もって感謝します。

4

1 に答える 1

2

- (PFQuery *)queryForTableまず、EventPFQueryTableViewControllerのメソッドを次のように変更することで、アプリがクラッシュしないようにすることができます。

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

お役に立てれば。

于 2014-07-11T02:17:45.270 に答える