English 中文(简体)
委托和导航控制器的问题
原标题:Problem with Delegate and NavigationController

我正在使用NavigationController从应用程序的rootView“推送”viewControllers。

我想使用委托来通信当前加载的视图和rootViewController。我可以使用NSNotificationCenter来做到这一点,但我想尝试一下这种特殊情况下的代表,因为沟通总是一对一的。

在推送的视图中,我在头文件中声明了以下委托协议:

#import <UIKit/UIKit.h>

@protocol AnotherViewControllerDelegate;

@interface AnotherViewController : UIViewController {
    id <AnotherViewControllerDelegate> delegate;
}

- (IBAction) doAction;

@property (nonatomic, assign) id delegate;

@end


@protocol AnotherViewControllerDelegate <NSObject>
- (void) doDelegatedAction:(AnotherViewController *)controller;
@end

doAction IBAction连接到视图中的UIButton。在我的实施文件中,我添加了:

#import "AnotherViewController.h"    
@implementation AnotherViewController

@synthesize delegate;

- (IBAction) doAction {
    NSLog(@"doAction");
    [self.delegate doDelegatedAction:self];
}

在我的RootViewController.h中,我将AnotherViewControllerDelegate添加到接口声明中:

@interface RootViewController : UIViewController <AnotherViewControllerDelegate> {...

这个到我的实现文件

- (void) doDelegatedAction:(AnotherViewController *)controller {
    NSLog(@"rootviewcontroller->doDelegatedAction");
 }

不幸的是,它不起作用。未调用rootViewController中的doDelegatedAction。我怀疑这是因为我推送AnotherViewController的方式:

    AnotherViewController *detailViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

我是否应该以任何方式告诉AnotherViewController,在它被推送的那一刻,它的委托将是RootViewController?还是我错过了什么?

最佳回答

您需要将AnotherViewController委托设置为rootViewController,以便所有内容都能正确连接。

如果您正在rootViewController中初始化AnotherViewController,则会是:

AnotherViewController *detailViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherViewController" bundle:nil];
detailViewController.delegate = self;
[self.navigationController pushViewController:detailViewController animated:YES];
问题回答

暂无回答




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...