我有一个视图控制器(UserLogin),它调用NSObject
类(custompop)中的一个方法。该方法返回一个 UIView 对象到viewController
. popupAction
在方法中,我添加一个按钮并在按钮单击时调用操作。视图控制器中的popupAction
调用方法。我设置了所有的委托属性。
这是代码:
//**in viewcontroller.h**
#import "custompopup.h"
@interface UserLogin : UIViewController<customPopUpDelegate>
{
custompopup *obj_custompopup;//NSObject Class
}
-(void)handlePopUpAction;
//**in viewcontroller.m**
- (void)viewDidLoad
{
[super viewDidLoad];
obj_custompopup = [[custompopup alloc] init];
[obj_custompopup setDelegate:self];
popupview = [[UIView alloc] init];
popupview = [obj_custompopup initwithTitleText:@"Title" withdelegate:self];
[self.view addSubview:popupview];
}
-(void)handlePopUpAction
{
NSLog(@"Inside handlePopUpAction");
}
//**in NSObject.h**
@protocol customPopUpDelegate <NSObject>
-(void)handlePopUpAction;
@end
@interface custompopup : NSObject
{
id<customPopUpDelegate>delegate;
UIButton *First_Btn;
}
@property(retain)id delegate;
//**in NSObject.m**
@synthesize delegate;
-(UIView *)initwithTitleText:(NSString *)titleText withdelegate:(id)del
//returns uiview to viewcontroller
{
self.delegate =del;
UIView *customView = [[UIView alloc] init];
customView.frame = CGRectMake(200, 100, 617,367);
First_Btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
First_Btn.frame=CGRectMake(20, 330,125,45);
[First_Btn @"title" forState:UIControlStateNormal];
[First_Btn addTarget:self action:@selector(popUpAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:First_Btn];
return customView;
}
-(void)popUpAction
{
[[self delegate] handlePopUpAction];
}
问题是编译器成功进入每个方法并在控制台中打印所有内容,直到最后一步。在视图控制器中完成最后一步后,EXC_BAD_ACCESS
应用程序崩溃。