【问题标题】:Custom delegate only works if directly called from UIButton selector ARC retain issue自定义委托仅在从 UIButton 选择器 ARC 保留问题直接调用时才有效
【发布时间】:2012-01-27 05:43:39
【问题描述】:

我正在尝试使用委托方法将视图推送到另一个视图。仅当此代码:[button addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside]; 更改为 [button addTarget:delegate action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside]; 时,该方法才有效并且不会为委托返回 null。否则,委托返回 null。我相信这是 ARC 没有在 ViewController 中保留 ContentViewController 的问题。请帮忙!

注意:我使用的是 ARC

ContentViewController

@class ContentViewController;
@protocol ContentViewControllerDelegate <NSObject>
@required
-(void)pushViewController:(UIViewController *)viewController;
@end

@interface ContentViewController : UIViewController 
{
    __weak id <ContentViewControllerDelegate> delegate;    
}

@property (weak) __weak id <ContentViewControllerDelegate> delegate;

- (void)pushView:(UIViewController *)viewController;

@end


**ContentViewController.m**

#import "ContentViewController.h"

@implementation ContentViewController

@synthesize delegate;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(0, 300, 250, 30);

    button.backgroundColor = [UIColor grayColor];

    [button setTitle:@"Test Button" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside]; 

    [self.view addSubview:button];

}

- (void)pushView:(UIViewController *)viewController
{             
    if([delegate respondsToSelector:@selector(pushViewController:)]) 
        [delegate pushViewController:viewController];

    NSLog(@"pushView");

}

视图控制器

#import "ContentViewController.h"

@interface ViewController : UIViewController <ContentViewControllerDelegate>
{
    IBOutlet UINavigationController *navigationController;
    __weak IBOutlet UIButton *navigationButton;
}

@property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
@property (strong, nonatomic) ContentViewController *contentViewController;

@end

#import "ViewController.h"
#import "BinViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation ViewController
@synthesize navigationController;
@synthesize contentViewController;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add the navigationController and set the frame
    self.navigationController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    [self.view addSubview:navigationController.view];

    // Init ContentViewController to set the delegate
    contentViewController = [[ContentViewController alloc] init];
    // Set the delegate
    contentViewController.delegate = self;

}

#pragma mark - ContentViewControllerDelegate Methods

-(void)pushViewController:(UIViewController *)viewController 
{    
    // This is here just to see if this instance method work ... I haven't actually sent it a view to push 
    BinViewController *binViewController = [[BinViewController alloc] init];
    [self.navigationController pushViewController:binViewController animated:YES];
}

编辑:这张图片应该有助于解释视图层次结构以及我在这里所做的事情。

【问题讨论】:

  • 如何呈现这个 ViewController 和 ContentViewController?哪个应该是导航控制器的根视图控制器?
  • 导航控制器的根视图是“FirstViewController”(这里我没有显示。) ContentViewController 显示在“自定义”模式视图中,该模式视图作为子视图添加到“ViewController”。如果您需要查看更多内容,请告诉我。
  • AppDelegate 对此有参考。我从单个视图模板开始,并将导航控制器添加到提供的“视图控制器”中。 View Controller 有一个 NavigationController,其根视图为“FirstViewController”。 “ViewController”将“ContentViewController”(当调用实例方法时)显示为子视图,我试图从 ContentViewController 将视图推送到 ViewController(我可以到 FirstViewController,但我认为这不会解决我的问题)。我将上传一张图片来解释视图层次结构。
  • 你还没有说你是如何推送 contentviewcontroller 的。我们可以看到您创建了它的一个新实例,并分配了委托,但您什么时候真正将它放在屏幕上?我怀疑您在屏幕上显示的是另一个实例,您没有设置委托。
  • 我从不推它。我相信我说过我用我创建的自定义模式视图实例添加它。我将它添加为 ViewController 的子视图,因此它可以占据整个屏幕并具有透明背景。想想在整个视图上滑出的 Flipboard iPad 应用程序“导航浏览器”。该视图是我的“ContentViewController”。我知道我可以使用标准模式视图将样式设置为 uimodalpresentationcurrentcontext 但这并没有按照我想要的方式工作。

标签: iphone objective-c ios delegates automatic-ref-counting


【解决方案1】:

最终使用 NSNotificationcenter 而不是自定义委托。感谢您的帮助。

编辑: 更改了我的应用程序以使用新的 iOS5 UIViewController Containment。好多了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多