Objective c ParentViewController和子视图:设置委托返回null
我有一个parentViewController和一个子视图,在视图中将出现在parentViewController中。我将子视图添加到self.view,并将子视图的委托设置为self。当我试图在子视图中使用委托来推送另一个视图控制器时,它不起作用,我检查了delegate的值,结果为null 我的主要想法是:当用户单击子视图中的按钮时,我需要使用parentViewController的navigationController按下另一个viewController 这是我的密码Objective c ParentViewController和子视图:设置委托返回null,objective-c,uiviewcontroller,delegates,subview,pushviewcontroller,Objective C,Uiviewcontroller,Delegates,Subview,Pushviewcontroller,我有一个parentViewController和一个子视图,在视图中将出现在parentViewController中。我将子视图添加到self.view,并将子视图的委托设置为self。当我试图在子视图中使用委托来推送另一个视图控制器时,它不起作用,我检查了delegate的值,结果为null 我的主要想法是:当用户单击子视图中的按钮时,我需要使用parentViewController的navigationController按下另一个viewController 这是我的密码 //par
//parentViewController.h
#import "mysubView.h"
@interface parentViewController : UIViewController <mysubViewDelegate>
.
.//Some properties here
.
@end
在mysubView.h中:
//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
使用mysubView而不是如下所示的GlobalObject
myView = [[mysubView loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];
创建新类时请使用大写字母。这是唯一的约定,但您应该遵守。以大写字母开始类,因此ParentViewController、newViewController……您创建了多少mysubView实例?我的类已经以大写字母开始,对此我很抱歉,但当我写这个问题时,我使用了小写字母。我只创建了一个mysubView实例@Wain@Sawsan你找到答案了吗?那是怎么回答这个问题的?!
@implementation mysubView
@synthesize delegate;
.
.
.
-(IBAction)ButtonClicked:(UIButton *)sender
{
newViewController *newController = [[newViewController alloc] initWithNibName:@"newViewController" bundle:nil];
//Here is my problem in this line .. self.delegate is null here
[self.delegate pushViewControllerUsingDelegate:newViewController];
}
-(void)pushViewControllerUsingDelegate:(UIViewController *)viewController
{
[self.delegate pushViewControllerUsingDelegate:viewController];
}
@end
myView = [[GlobalObjects loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];
myView = [[mysubView loadNibNamed:@"mysubView" owner:self options:nil] objectAtIndex:0];