gpt4 book ai didi

ios - 为什么不在 objective-c 中工作委托(delegate)?

转载 作者:行者123 更新时间:2023-11-29 02:43:07 25 4
gpt4 key购买 nike

我在 OMMenuTableViewController.h 中放置了一个协议(protocol):

//
// 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 中调用委托(delegate)

//
// 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 中的委托(delegate)

//
// 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

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

最佳答案

OMOrdersTableViewController 类中,如果你想接收委托(delegate)回调,你应该在那个类中有 OMMenuTableViewController 的实例并分配 delegate对象 self

关于ios - 为什么不在 objective-c 中工作委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25504179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com