1

私はoauth2.0を使用してGoogleドキュメントに接続するアプリに取り組んでいます。ユーザーが接続すると、ユーザーはtableViewに移動するボタンをクリックしてすべてのドキュメントを表示します。このために、私はセグエを使用してデータを転送しています。ここで、ユーザーが1つのドキュメントを選択し、それにチェックマークを付けて、データを渡すビューコントローラーに戻り、ドキュメントが選択されたものを表示するようにします。データを返すには、プロトコルとデリゲートを使用する必要があることを読みました。私はこれに従いました:View Controller間でデータを渡すが、私の問題は、delegateメソッドが呼び出されていないことです。

これが私のストーリーボードです。2つのビュー、viewcontrollerと、と呼ばれるtablaviewvcontrollerVistaTableViewControllerです。

ここに画像の説明を入力してください

ユーザーが認証ボタンを押すと、oauthを使用してGoogleドキュメントに接続します。彼が「Listar」ボタンを押すと、VistaTableViewControllerが表示されます。

プロトコルを定義するVistaTableViewController.hのコードは次のとおりです。

#import <UIKit/UIKit.h>
#import "GData.h"


@class VistaTableViewController;

@protocol VistaTableViewControllerDelegate <NSObject>
- (void)loqueselecciono:(VistaTableViewController *)controller didSelectDoc:(NSString *)documento;
@end


@interface VistaTableViewController : UITableViewController <UITableViewDataSource>
{
    IBOutlet UITableView *tablalistar;
    GDataFeedDocList *mDocListFeed2;
}

@property (nonatomic, weak) id <VistaTableViewControllerDelegate> delegate;
@property (nonatomic, strong) NSString *documento;
@property (nonatomic, retain) GDataFeedDocList *mDocListFeed2;

@end

デリゲートメソッドを呼び出すVistaTableViewController.mのコードは次のとおりです。

#import "VistaTableViewController.h"


@implementation VistaTableViewController
{
    NSInteger selectedIndex;
}

@synthesize delegate;
@synthesize documento;
@synthesize mDocListFeed2;

- (void)viewDidLoad
{
    [super viewDidLoad];

    selectedIndex = [[mDocListFeed2 entries] indexOfObject:self.documento];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[mDocListFeed2 entries] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tablalistar"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tablalistar"];
    }

    GDataEntrySpreadsheet *doc = [[mDocListFeed2 entries] objectAtIndex:indexPath.row];

    cell.textLabel.text = [[doc title] stringValue];

    if (indexPath.row == selectedIndex)
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (selectedIndex != NSNotFound)
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    selectedIndex = indexPath.row;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    GDataEntrySpreadsheet *doc = [[mDocListFeed2 entries] objectAtIndex:indexPath.row];

    NSString *theDoc = [[doc title] stringValue];

    [self.delegate loqueselecciono:self didSelectDoc:theDoc];
}

@end

では、ViewControllerにVistaTableViewControllerをインポートし、そのプロトコルに準拠するように指示する必要があります。

これがViewController.hのコードです

#import <UIKit/UIKit.h>
#import "GTMOAuth2ViewControllerTouch.h"
#import "GData.h"
#import "VistaTableViewController.h"

@interface ViewController : UIViewController <VistaTableViewControllerDelegate>

- (GDataServiceGoogleDocs *)docsService;
- (void)authorize;
- (void) mifetch;
- (IBAction)autenticarse;
- (IBAction)listar:(id)sender;
- (void) ticket: (GDataServiceTicket *) ticket finishedWithFeed: (GDataFeedDocList *) feed error: (NSError *) error;

@property (nonatomic, retain) NSString *accessToken;
@property (nonatomic, retain) GDataFeedDocList *mDocListFeed;
@property (nonatomic, retain) GDataServiceTicket *mDoclistFetchTicket;


@end

次に、Viewcontroller.mで、デリゲートメソッドを次のように実装します(選択されたドキュメントのタイトルを示すアラートを表示したいだけです)。

- (void)loqueselecciono:(VistaTableViewController *)controller didSelectDoc:(NSString *)documento;
{
    [self.navigationController popViewControllerAnimated:YES];

    UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:@"doc seleccionado"
                                                         message:[NSString stringWithFormat:@"titulo: %@", documento]
                                                        delegate:self
                                               cancelButtonTitle:@"Dismiss"
                                               otherButtonTitles:nil];

    [alertView show];
}

また、Viewcontroller.mでも、デリゲートに自分自身を割り当てるためにこれを作成しました。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"displaydocs"])
    {
        VistaTableViewController *vistatableview = [[VistaTableViewController alloc] init];

        vistatableview.delegate = self;

        vistatableview = [segue.destinationViewController performSelector:@selector(setMDocListFeed2:) withObject:mDocListFeed];
    }
}

さて、すべてのドキュメントを表示するde tableViewを取得し、ユーザーはチェックマークを表示するドキュメントを選択できますが、デリゲートメソッドが呼び出されていないため、viewcontrollerビューに戻らず、データが返されません。

何が足りないの?ありがとう!!

4

1 に答える 1

0

あなたが書くとき、VistaTableViewController *vistatableview = [[VistaTableViewController alloc] init];あなたは新しいオブジェクトを作成しています。本当にやりたいことは、 を使用しsegue.destinationViewControllerそのデリゲートを にVistaTableViewController設定することです。self

そのままでは、デリゲートを持つオブジェクトは、プッシュされてビュー ロジックを処理するオブジェクトではありません。

(また、setMDocListFeed2オブジェクトを返さないため、割り当てperformSelectorはかなり誤解を招きます。)

于 2012-07-17T13:57:11.023 に答える