【问题标题】:Delegate not working, when using a modal presented NavigationController ( iOS 5 - Storyboard )使用模态呈现的 NavigationController(iOS 5 - Storyboard)时,委托不起作用
【发布时间】:2011-12-07 14:03:52
【问题描述】:

我的协议有问题。

我的“初始视图控制器”是一个导航控制器。在根页面上,我显示了另一个导航控制器,其中嵌入了视图控制器。 onclick 一个 segue 应该被触发......这很有效,但是从“ViewController”的委托方法永远不会被调用。

我添加的图像是我如何在 iOS 5 中使用 InterfaceBuilder 在 2 个 NavigationController 之间建立连接的示例。

MyViewController.h

@protocol MyProtocol <NSObject>

@required
- (void) myFunction:(NSString *)string;

@end

@interface MyViewController : UIViewController <MyProtocol>

@property (nonatomic, assign) id<MyProtocol> delegate;

@end

MyViewController.m

#import "MyViewController.h"
@implementation PropertyController
@synthesize delegate = _delegate;

- (void) myFunction:(NSString *)string {
    [_delegate myFunction:string];
}

- (IBAction) callDelegate:(id)sender {
     ![enter image description here][1][self myFunction:@"test"];
}

这是从上方显示 NavigationController 的 ViewController 的代码

ViewController.h

#import "MyViewController.h"

@interface ViewController : UIViewController <MyProtocol>

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void) myFunction:(NSString *)string {
    NSLog(@"myFunction was called");
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [((MyViewController *) segue.destinationViewController) setDelegate:self];
}

- (IBAction) showModalNavigationController {
    [self performSegueWithIdentifier:@"NameFromSegueInInterfaceBuilder" sender:self];
}

我找不到解决问题的方法。

希望有人能帮帮我

谢谢你:)

【问题讨论】:

    标签: delegates uinavigationcontroller interface-builder ios5 segue


    【解决方案1】:

    我在这里看到了几个问题:

    1. 在您的故事板屏幕截图中,您有第二个导航控制器。您不需要将 PropertyController 嵌入导航控制器中。相反,让根视图控制器直接连接到 PropertyController。如果出于某种原因您确实需要该导航控制器,那么您需要更改上面的prepareForSegue 实现,因为在这种情况下segue.destinationViewController 指向UINavigationController。因此,您需要获取该导航控制器对象,然后将setDelegate 发送到该导航控制器对象的rootViewController。但同样,前提是您决定保留该导航控制器。
    2. MyViewController 与您的ViewControllerPropertyController 类有什么关系? PropertyController 类(或超类)需要具有 delegate 属性的 @propertysynthesize 语句。

    【讨论】:

    • 嗨,谢谢,但我需要第二个导航控制器。所以我必须改变 prepareForSegue 的实现。我如何在 MyViewController 那里设置委托?
    • 由于这是处理故事板时的常见需求,我为此在UIStoryboardSegue 上实现了一个类别。请参阅我的 answer 到另一个故事板问题。
    • 非常感谢!现在我将 topViewController 的委托设置为 self ,现在它可以工作了! :) [((MyViewController *)(((UINavigationController *) segue.destinationViewController).topViewController)) setDelegate:self];
    【解决方案2】:

    我遇到了同样的问题,为了让它工作,我找到了一个 xcode 不太喜欢的工作,但它仍然可以工作 --> 在你做的时候设置你的委托,使你的导航控制器将您的视图作为委托,如果您在目标控制器中使用 NSLog self.delegate 它将为空。为了防止这种情况发生-->

    self.delegate = self.navigationController.delegate; 
    

    在您的目标控制器 (viewdidload) 中,它将获取您在导航控制器上创建的委托,并允许您在目标控制器中使用它

    它很脏,但这是我找到的唯一方法。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多