ParentViewController和子视图:设置委托返回null

时间:2014-01-06 10:56:01

标签: objective-c uiviewcontroller delegates subview pushviewcontroller

我在parentViewController的{​​{1}}中有一个subView和一个viewWillAppear我已将parentViewController添加到subview我设置了self.view的子视图delegate。当我试图在子视图中使用委托给self另一个视图控制器它不起作用时,我检查了委托的值并且它得到了“null”。

我的主要想法:当用户点击子视图中的按钮时,我需要使用parentViewController的push推送另一个viewController。

这是我的代码

navigationController

在实施文件中:

//parentViewController.h 

#import "mysubView.h"

@interface parentViewController : UIViewController <mysubViewDelegate>
.
.//Some properties here
.
@end

在mysubView.h中:

//parentViewController.m 

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    mysubView *myView;

    //GlobalObjects is to load the file in a certain language
    myView = [[GlobalObjects loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];

    //Print myView.delegate gets the correct value here
    myView.delegate = self;

    [self.view addSubview:myView];

}
.
.
.
-(void)pushViewControllerUsingDelegate:(UIViewController *)viewController
{
    [self.navigationController pushViewController:viewController animated:YES];
}

在mysubView.m

//mysubView.h

@protocol mysubViewDelegate <NSObject>

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

@end

@interface mysubView : UIView< UITableViewDataSource, UITableViewDelegate>
{
}

@property (nonatomic , retain) id <mysubViewDelegate> delegate;

-(IBAction)ButtonClicked:(UIButton *)sender;


@end

1 个答案:

答案 0 :(得分:0)

myView = [[GlobalObjects loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];

使用mysubView代替GlobalObjects,如下所示

myView = [[mysubView loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];

创建新课时请使用大写字母。这只是惯例,但您应该遵循它。