2011-12-24 15 views
2

我正在嘗試使用委託方法將視圖推送到另一個視圖。該方法僅適用於此代碼:[button addTarget:self action:@selector(pushView:) forControlEvents:UIControlEventTouchUpInside];更改爲[button addTarget:delegate action:@selector(pushViewController:) forControlEvents:UIControlEventTouchUpInside];時,代理不返回null。否則,委託返回null。我相信這是ARC沒有在ViewController中保留ContentViewController的問題。請幫忙!自定義委託只適用於直接從UIButton選擇ARC調用保留問題

注:我使用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]; 
} 

編輯:此圖片應該有助於解釋視圖層級和我在這裏做什麼。 enter image description here

+0

你如何呈現這個ViewController和ContentViewController?哪個應該是導航控制器的根視圖控制器? – 2011-12-24 07:21:20

+0

導航控制器的根視圖是「FirstViewController」(我沒有在這裏顯示)。ContentViewController顯示在「自定義」模式視圖中,並作爲子視圖添加到「ViewController」。如果您需要了解更多信息,請告訴我。 – morcutt 2011-12-24 07:29:56

+0

AppDelegate有這個參考。我開始使用單個視圖模板,並將導航控制器添加到提供的「ViewController」中。 View Controller有一個NavigationController,其根視圖爲「FirstViewController」。 「ViewController」顯示「ContentViewController」(當一個實例方法被調用時)作爲子視圖,我試圖從ContentViewController向ViewController推送一個視圖(我可以給FirstViewController,但我認爲這不會解決我的問題)。我將上傳一張圖片來解釋視圖層次結構。 – morcutt 2011-12-24 07:45:52

回答

0

使用NSNotificationcenter而不是自定義委託結束。感謝您的幫助。

編輯:更改我的應用程序使用新的iOS5 UIViewController遏制。好多了!