委托方法未被viewController调用

时间:2012-10-11 03:33:13

标签: objective-c ios xcode delegates protocols

所以这就是发生了什么(除了我是菜鸟之外)。

我有两个视图控制器,AddAPlaceViewController和showCategoriesViewController。

我在AddAPlaceVC中有一个表格单元格,你可以点击它和showCategoriesVC(这是一个tableviewcontroller)打开(模态)并给你一个十个选项列表。您点按其中一个或点按取消按钮。

我花了好几天了解并找出协议和代表。我想我已经把它们设置正确,但无法弄清楚它们为什么不工作或被调用。我已经尝试过各种各样的改变,但到目前为止还没有运气。

你们是我唯一的希望! :)

以下是代码的重要部分:

AddAPlaceViewController.h

AddAPlaceViewController.h

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


@interface AddAPlaceViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UITableViewDataSource, UITableViewDelegate, ShowCategoriesViewControllerDelegate>

AddAPlaceViewController.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");

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

参考Link