我正在尝试使用委托方法将视图推送到另一个视图。只有当此代码:[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");
}
的ViewController
#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];
}
编辑:此图片应该有助于解释视图层次结构以及我在这里做的事情。
答案 0 :(得分:0)
使用NSNotificationcenter而不是自定义委托结束。谢谢你的帮助。
编辑:将我的应用程序更改为使用新的iOS5 UIViewController Containment。好多了!