Objective C:使用委托调用父类中的函数

发布于 2024-10-19 10:45:53 字数 3131 浏览 1 评论 0原文

我正在创建一个 3 层导航弹出控制器,在第三个弹出控制器上,我有一个委托方法来访问父类中的 dismissPopup 方法。我似乎无法调用它,我的父类函数中的 NSLog 消息甚至没有显示,所以我要么使用了错误的委托,要么错误地调用了它。

ParentViewController 的 3 个类有一个工具栏,其中有一个按钮可以调出表格视图 --> RegionViewController 是第一个带有项目的表视图控制器 --> ConusViewController 是推入导航堆栈的第二个表视图控制器。我试图在单击选择后通过委托调用父方法中的 dismissPopover 方法,以便整个弹出窗口消失。

在 ConusViewController 中,如果委托有效,我会从父类中的函数中看到“方法访问”。它没有显示,所以我一定是错误地使用了委托。

很抱歉我的帖子如此冗长,我想完成我在这里想做的事情。谢谢。

ParentViewController.h

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

@interface EnscoWXViewController : UIViewController <ConusViewControllerDelegate> {
    UIPopoverController *popoverController;
    IBOutlet UIWebView *webImageDisplay;
    ConusViewController *cViewController;
}
@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) UIWebView *webImageDisplay;

@property (nonatomic, retain) ConusViewController *cViewController;

-(IBAction) buttonShowRegion:(id) sender;
@end

ParentViewController.m

#import "ParentViewController.h"
#import "RegionViewController.h"

@implementation ParentViewController
@synthesize cViewController;

-(IBAction) buttonShowRegion:(id) sender {
...
}

-(void)dismissPopover {
    [popoverController dismissPopoverAnimated:YES];
    printf("Method Accessed\n");
}

- (void)viewDidLoad {
    cViewController = [[ConusViewController alloc] init];
    cViewController.delegate = self;
    [super viewDidLoad];
}

RegionViewController.m >

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0: { 
            ConusViewController *conusViewController = [[ConusViewController alloc] initWithNibName:@"ConusViewController" bundle:nil];
            conusViewController.contentSizeForViewInPopover = CGSizeMake(320, 350);
            [self.navigationController pushViewController:conusViewController animated:YES];
            [conusViewController release];
            break;
        }
        case 1: {
            break;
        }
    }
}

ConusViewController.h

#import <UIKit/UIKit.h>

@protocol ConusViewControllerDelegate <NSObject>
@required
- (void)dismissPopover;
@end

@interface ConusViewController : UITableViewController {
    NSMutableArray *conusItems;
    id delegate;
}
@property (nonatomic, assign) id <ConusViewControllerDelegate> delegate ;
@end

ConusViewController.m

#import "ConusViewController.h"
#import "ParentWXViewController.h"

@implementation ConusViewController
@synthesize delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *itemRequested = [conusItems objectAtIndex:indexPath.row];
    NSLog(@"logging: %@", itemRequested);
    [delegate dismissPopover];
    [itemRequested release];
}

I'm creating a 3 layer navigation popup controller and on the 3rd popup controller I have a delegate method to access dismissPopup method that is in the parent class. I can't seem to call it, my NSLog messages in the function in the parent class isn't even showing so I must be either using delegation wrong or I'm calling it incorrectly.

The 3 classes ParentViewController has a toolbar with a button that brings up the table view --> RegionViewController is the First table view controller with items --> ConusViewController is the 2nd table view controller that is pushed onto the navigation stack. I'm trying to call the method dismissPopover that is in the parent method with a delegation after the selection is clicked on so the whole popover goes away.

In the ConusViewController if the delegation had worked I would have seen "Method Accessed" from the function in the parent class. It doesn't show so I must be using delegation wrong.

Sorry for being so wordy on my post, I wanted to be complete on what I'm trying to do here. Thanks.

ParentViewController.h

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

@interface EnscoWXViewController : UIViewController <ConusViewControllerDelegate> {
    UIPopoverController *popoverController;
    IBOutlet UIWebView *webImageDisplay;
    ConusViewController *cViewController;
}
@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) UIWebView *webImageDisplay;

@property (nonatomic, retain) ConusViewController *cViewController;

-(IBAction) buttonShowRegion:(id) sender;
@end

ParentViewController.m

#import "ParentViewController.h"
#import "RegionViewController.h"

@implementation ParentViewController
@synthesize cViewController;

-(IBAction) buttonShowRegion:(id) sender {
...
}

-(void)dismissPopover {
    [popoverController dismissPopoverAnimated:YES];
    printf("Method Accessed\n");
}

- (void)viewDidLoad {
    cViewController = [[ConusViewController alloc] init];
    cViewController.delegate = self;
    [super viewDidLoad];
}

RegionViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0: { 
            ConusViewController *conusViewController = [[ConusViewController alloc] initWithNibName:@"ConusViewController" bundle:nil];
            conusViewController.contentSizeForViewInPopover = CGSizeMake(320, 350);
            [self.navigationController pushViewController:conusViewController animated:YES];
            [conusViewController release];
            break;
        }
        case 1: {
            break;
        }
    }
}

ConusViewController.h

#import <UIKit/UIKit.h>

@protocol ConusViewControllerDelegate <NSObject>
@required
- (void)dismissPopover;
@end

@interface ConusViewController : UITableViewController {
    NSMutableArray *conusItems;
    id delegate;
}
@property (nonatomic, assign) id <ConusViewControllerDelegate> delegate ;
@end

ConusViewController.m

#import "ConusViewController.h"
#import "ParentWXViewController.h"

@implementation ConusViewController
@synthesize delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *itemRequested = [conusItems objectAtIndex:indexPath.row];
    NSLog(@"logging: %@", itemRequested);
    [delegate dismissPopover];
    [itemRequested release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-10-26 10:45:53

在调用 [delegatemissPopover] 之前,检查 delegate 是否实际设置。可能不是。

我在 ParentViewController.m 中看到您创建了 ConusViewController 的实例并设置其委托,但从不显示它。在 RegionViewController.m 中,您创建了 ConusViewController 的另一个实例,而不设置其委托,并且似乎是正在显示的实例。

Just before calling [delegate dismissPopover], check if delegate is actually set. It probably isn't.

I see in ParentViewController.m you create an instance of ConusViewController and set its delegate, but never display it. In RegionViewController.m you create another instance of ConusViewController without setting its delegate and that is the one that seems to be being displayed.

故事↓在人 2024-10-26 10:45:53

不确定我是否错过了它,但我从未见过你在 ConusViewController 中设置委托属性。需要将其设置为要委托给的对象的实例(其中实现了dismissPopover 的对象)。

Not sure if I missed it, but I never see you set the delegate property in ConusViewController. That needs to be set to an instance of the object that is to be delegated to (the object that has dismissPopover implemented in it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文