[英]EXC_BAD_ACCESS in UIViewcontroller when Method calling from NSObject
我有一個視圖控制器(UserLogin),它調用NSObject
類(custompop)中的方法。 該方法將UIView對象返回給viewController
。 在方法中,我添加一個按鈕,並在按鈕單擊時調用action 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
出現,應用程序崩潰。
你的初始化是錯誤的..沒有得到你想做的事情,但是你返回了customView,保留了self,但是你正在使用self。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.