我有以下代码用于按钮创建和按钮操作,我正在调用buildUI方法
CGRect cgRct = CGRectMake(10 ,30 ,400, 320); //define size and position of view
subMainView_G_obj = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
subMainView_G_obj.autoresizesSubviews = YES;
//set view property ov controller to the newly created view
// create Button's for modules in array (UIButtonTypeRoundedRect)
UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIButton_G_obj.frame = CGRectMake(100,30,100,50);
[UIButton_G_obj setTitle:@"UI" forState:UIControlStateNormal];
UIButton_G_obj.backgroundColor = [UIColor clearColor];
[subMainView_G_obj addSubview:UIButton_G_obj];
//[UIButton_G_obj setEnabled:TRUE];
[UIButton_G_obj addTarget:subMainView_G_obj action:@selector(buildUIWorkArea) forControlEvents:UIControlEventTouchUpInside];
[mainView_G_obj addSubview:subMainView_G_obj];
} - (无效)buildUIWorkArea { // UIView * uiWorkAreaView_G_obj; [uiWorkAreaView_G_obj clearsContextBeforeDrawing]; CGRect cgRct2 = CGRectMake(0.0,0.0,480,320); //定义View的大小和位置 uiWorkAreaView_G_obj = [[UIView alloc] initWithFrame:cgRct2]; //初始化视图 uiWorkAreaView_G_obj.autoresizesSubviews = YES;
(UIButtonTypeRoundedRect)buttonUIObj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
buttonUIObj.frame = CGRectMake(100.30,100,50);
[buttonUIObj setTitle:BUTTON forState:UIControlStateNormal];
buttonUIObj.backgroundColor = [UIColor clearColor];
[uiWorkAreaView_G_obj addSubview:buttonUIObj];
[buttonUIObj addTarget:uiWorkAreaView_G_obj action:@selector(showModuleView:) forControlEvents:UIControlEventTouchUpInside];
[mainView_G_obj clearsContextBeforeDrawing];
[mainView_G_obj addSubview:uiWorkAreaView_G_obj];
}
点击UIView uiWorkAreaView_G_obj上的按钮UI,它必须在UIView uiWorkAreaView_G_obj上创建另一个按钮BUTTON。在按下每个子视图的按钮后,我将这些子视图放在UIView类型的公共mainview_G_obj上。 但它抛出一个异常.......
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView buildUIWorkArea]:unrecognized selector sent to instance 0x57ca80'
我不明白为什么它将buildUIWorkArea的返回类型作为UIView,当我将其返回类型声明为void时。
帮助。
答案 0 :(得分:0)
[UIButton_G_obj addTarget:subMainView_G_obj action:@selector(buildUIWorkArea)
forControlEvents:UIControlEventTouchUpInside];
此行表示当用户触摸按钮内部时,将调用以下函数:
[subMainView_G_obj buildUIWorkArea];
由于subMainView_G_obj
是一个简单的UIView,因此没有-buildUIWorkArea
方法,因而错误。 -buildUIWorkArea
的返回类型无关紧要。
您可能希望将-buildUIWorkArea
发送至self
:
[UIButton_G_obj addTarget:self action:@selector(buildUIWorkArea)
forControlEvents:UIControlEventTouchUpInside];