我有一个视图控制器(UserLogin),它调用NSObject
类(custompop)中的方法。该方法将UIView对象返回到viewController
。在方法i中,添加一个按钮并在按钮单击时调用操作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
出现,应用程序崩溃。
答案 0 :(得分:0)
你的初始化错误..不要得到你尝试做的但是你返回自定义视图,留下自我保留,但你正在使用自己。