所以这就是正在发生的事情(除了我是一个菜鸟)。
我有两个视图控制器,AddAPlaceViewController 和 showCategoriesViewController。
我在 AddAPlaceVC 中有一个表格单元格,您点击它并打开 showCategoriesVC(它是一个表格视图控制器)(模式)并为您提供十个选项的列表。您点击其中一个或点击取消按钮。
我花了几天时间来理解和弄清楚协议和代表。我想我已经正确设置了它们,但无法弄清楚为什么它们不工作或被呼叫。我已经尝试了各种改变,但到目前为止没有运气。
你们是我唯一的希望!:)
这是代码的重要部分:
添加APlaceViewController.h
AddAPlaceViewController.h
#import <UIKit/UIKit.h>
#import "showCategoriesViewController.h"
@interface AddAPlaceViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate, ShowCategoriesViewControllerDelegate>
添加APlaceViewController.m
AddAPlaceViewController.m
#import "AddAPlaceViewController.h"
#import "FSQVenue.h"
#import "Categories.h"
//#import "showCategoriesViewController.h"
#import <QuartzCore/QuartzCore.h>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Un-highlight the selected cell
[categoryTable deselectRowAtIndexPath:indexPath animated:YES];
showCategoriesViewController *SCVC = [[showCategoriesViewController alloc] init];
SCVC = [self.storyboard instantiateViewControllerWithIdentifier:@"showCategories"];
SCVC.categoryDelegate = self;
[self.navigationController presentModalViewController:SCVC animated:YES];
}
#pragma mark showCategoriesViewControllerDelegate
-(void)didSelectOptions:(NSInteger )selectedCategory
{
NSInteger category = selectedCategory;
NSLog(@"Add Places %d", category);
[self dismissModalViewControllerAnimated:YES];
}
-(void)didCancelOptions
{
NSLog(@"Add Places -- Dismiss Button Pressed");
[self dismissModalViewControllerAnimated:YES];
}
showCategoriesViewController.h
showCategoriesViewController.h
#import <UIKit/UIKit.h>
@protocol ShowCategoriesViewControllerDelegate;
@interface showCategoriesViewController : UITableViewController
{
id<ShowCategoriesViewControllerDelegate>categoryDelegate;
}
@property (nonatomic, weak) id<ShowCategoriesViewControllerDelegate> categoryDelegate;
- (IBAction)cancelButton:(id)sender;
@end
@protocol ShowCategoriesViewControllerDelegate <NSObject>
- (void)didSelectOptions:(NSInteger )selectedCategory;
- (void)didCancelOptions;
@end
showCategoriesViewController.m
showCategoriesViewController.m
#import "showCategoriesViewController.h"
#import "Categories.h"
@interface showCategoriesViewController ()
@property (strong, nonatomic) NSArray *categoryArray;
@end
@implementation showCategoriesViewController
@synthesize categoryDelegate = _categoryDelegate;
@synthesize categoryArray;
…
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[_categoryDelegate didSelectOptions:indexPath.row];
NSLog(@"Selected IndeXPath %@", cell);
NSLog(@"Selected IndeXPath GOING TO DELEGATE %d", indexPath.row);
}
- (IBAction)cancelButton:(id)sender {
[_categoryDelegate didCancelOptions];
NSLog(@"Button Pressed");
}