为什么不在objective-c中工作代表?

时间:2014-08-26 10:59:50

标签: ios objective-c

我将协议放在 OMMenuTableViewController.h 中:

//
//  OMMenuTableViewController.h
//  iRepair
//
//  Created by Олег Мельник on 17.08.14.
//  Copyright (c) 2014 oleg melnik. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol OMLoadOrdersDelegate;

@interface OMMenuTableViewController : UITableViewController

@property (assign, nonatomic) id <OMLoadOrdersDelegate> delegate;
@property (weak, nonatomic) NSMutableArray* categoryArray;
@property (strong, nonatomic) IBOutlet UITableView *tableViewOutlet;
@property (strong, nonatomic) NSString* currentString;

@end

@protocol OMLoadOrdersDelegate

- (void) loadOrders;

@end

我在 OMMenuTableViewController.m中调用委托

//
//  OMMenuTableViewController.m
//  iRepair
//
//  Created by Олег Мельник on 17.08.14.
//  Copyright (c) 2014 oleg melnik. All rights reserved.
//

#import "OMMenuTableViewController.h"
#import "OMMenuTableViewCell.h"
#import "OMVariables.h"
#import <Parse/Parse.h>

@implementation OMMenuTableViewController

- (void)viewDidLoad
{
    PFUser *currentUser = [PFUser currentUser];
    if(currentUser){
        NSLog(@"User %@ logged in", [PFUser currentUser].username);
    }else{
        [self performSegueWithIdentifier:@"showLogin" sender:nil];
    }
    [self.delegate loadOrders];
    [super viewDidLoad];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    self.currentString = cell.textLabel.text;
    [self performSegueWithIdentifier:@"categorySegue" sender:nil];
    return indexPath;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"categorySegue"]) {
        [OMVariables sharedVariable].parseClassNameVaribale = self.currentString;
        [OMVariables sharedVariable].tableViewTitleVariable = self.currentString;
        [OMVariables sharedVariable].parseClassNameVaribale = [[OMVariables sharedVariable].parseClassNameVaribale stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}



@end

我在 OMOrdersTableViewController.h

中订阅了一个委托
//
//  OMOrdersTableViewController.h
//  iRepair
//
//  Created by Олег Мельник on 18.08.14.
//  Copyright (c) 2014 oleg melnik. All rights reserved.
//

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

@interface OMOrdersTableViewController : UITableViewController <UIActionSheetDelegate, OMLoadOrdersDelegate> {
    NSMutableArray* ordersArray;
}

@property (strong, nonatomic) IBOutlet UITableView *tableViewOutlet;

- (IBAction)refreshOrdersAction:(id)sender;
- (IBAction)sendOrderAction:(id)sender;

- (void) loadOrders;

@end

我在 OMOrdersTableViewController.m

中实施了 loadOrders方法
//
//  OMOrdersTableViewController.m
//  iRepair
//
//  Created by Олег Мельник on 18.08.14.
//  Copyright (c) 2014 oleg melnik. All rights reserved.
//

#import "OMOrdersTableViewController.h"
#import "OMOrdersTableViewCell.h"
#import "OMMenuTableViewController.h"
#import <Parse/Parse.h>

@interface OMOrdersTableViewController ()

@end

@implementation OMOrdersTableViewController

@synthesize tableViewOutlet;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [self loadOrders];
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Parcing 

- (void) loadOrders
{
    NSLog(@"delegated");
    PFQuery *retrieve = [PFQuery queryWithClassName:@"preOrder"];
    [retrieve whereKey:@"user" equalTo:[PFUser currentUser]];
    [retrieve findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (error) {
            NSLog(@"Error with initial query: %@ %@", error, [error userInfo]);
        } else {
            ordersArray = [[NSMutableArray alloc] initWithArray:objects];
        }
        NSLog(@"%@", ordersArray);

        if (ordersArray.count > 0) {
            [[[[[self tabBarController] viewControllers]
               objectAtIndex:1] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%lu", (unsigned long)ordersArray.count]];

            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:ordersArray.count];

        } else {
            [[[[[self tabBarController] viewControllers] objectAtIndex:1] tabBarItem] setBadgeValue:nil];
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        }


        [tableViewOutlet reloadData];
    }];
}
//another code

为什么我的代表不工作?如何解决我的问题?

2 个答案:

答案 0 :(得分:1)

OMOrdersTableViewController类中,如果您想接收委托回调,那么您应该在该类中拥有OMMenuTableViewController个实例,并将delegate对象分配给{{1} }。

答案 1 :(得分:0)

您似乎从未设置delegate的{​​{1}}属性。 OMMenuTableViewController实现了OMOrdersTableViewController协议,但OMLoadOrdersDelegate从未设置为委托。