0

所以这就是正在发生的事情(除了我是一个菜鸟)。

我有两个视图控制器,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");

}
4

2 回答 2

0

由于您有 presentModalViewController: 您的SCVC,因此您的AddAPlaceViewController处于非活动状态。如果您可以使用自定义委托方法将 showCategoriesViewController 创建为 UITableView 的子类,那就更好了。

参考链接

于 2012-10-11T03:48:00.290 回答
0

最有可能的猜测?在您发布的代码中,您的属性和您的 ivar 都被称为categoryDelegate,但您将您的属性视为引用了一个名为 的 ivar _categoryDelegate。可能这只是一个错字,但是因为 iOS 允许调用空对象上的方法(并且什么也不做),所以它会默默地失败。将 ivar 重命名为_categoryDelegate应该可以修复它。

于 2012-10-11T03:40:04.173 回答