我在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
答案 0 :(得分:0)
myView = [[GlobalObjects loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];
使用mysubView代替GlobalObjects,如下所示
myView = [[mysubView loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];
创建新课时请使用大写字母。这只是惯例,但您应该遵循它。