gpt4 book ai didi

iphone - 自定义委托(delegate)仅在从 UIButton 选择器 ARC 保留问题直接调用时才有效

转载 作者:行者123 更新时间:2023-12-01 16:59:19 25 4
gpt4 key购买 nike

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

注意:我正在使用 ARC

内容 View Controller

@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");

}

View Controller
#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];
}

编辑:这张图片应该有助于解释 View 层次结构和我在这里做什么。
enter image description here

最佳答案

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

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

关于iphone - 自定义委托(delegate)仅在从 UIButton 选择器 ARC 保留问题直接调用时才有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8623257/

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