我正在开发一个使用 oauth 2.0 与谷歌文档连接的应用程序,一旦用户连接,他单击一个按钮,该按钮转到 tableView 以显示所有文档。为此,我正在使用 segue 并向前传递数据。现在我希望用户选择一个文档,勾选它并返回到视图控制器,将数据传回并显示选择了哪个文档。我读到要传回数据,我需要使用协议和委托。我遵循了这个:在视图控制器之间传递数据,但我的问题是我的委托方法没有被调用。
这是我的故事板,只有 2 个视图,一个视图控制器和一个名为VistaTableViewController
.
当用户按下身份验证按钮时,他使用 oauth 连接到谷歌文档。当他按下“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 中,我也写了这个来将 self 分配给委托。
- (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 显示所有文档,用户可以选择一个显示 de 复选标记但未调用委托方法,因此它不会返回到 viewcontroller 视图并且没有数据传回。
我错过了什么??谢谢!!