简体   繁体   中英

Terminating app due to uncaught exception 'NSInvalidArgumentException', in iPhone programming

i've the following code for button creation and on action of button , i'm calling buildUI method

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];

} -(void)buildUIWorkArea { //UIView* uiWorkAreaView_G_obj; [uiWorkAreaView_G_obj clearsContextBeforeDrawing]; CGRect cgRct2 = CGRectMake(0.0, 0.0, 480, 320); //define size and position of View uiWorkAreaView_G_obj = [[UIView alloc] initWithFrame:cgRct2]; //initilize the view 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];

}

On click of button UI which is on UIView uiWorkAreaView_G_obj , it has to create one more button BUTTON on UIView uiWorkAreaView_G_obj.and after placing the button each of there subviews, i'm placing those subviews upon a common mainview_G_obj of type UIView. But its throwing an exception .......

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView buildUIWorkArea]:unrecognized selector sent to instance 0x57ca80' 

i didn't understand why its taking buildUIWorkArea's return type as UIView , when i've declared its return type as void.

plz help.

[UIButton_G_obj addTarget:subMainView_G_obj action:@selector(buildUIWorkArea)
         forControlEvents:UIControlEventTouchUpInside];

This line means that, when the user touch-up-inside the button, the following function will be called:

[subMainView_G_obj buildUIWorkArea];

Since subMainView_G_obj is a plain UIView, there's no -buildUIWorkArea method and thus the error. The return type of -buildUIWorkArea is irrelevant.

You probably want to send -buildUIWorkArea to self instead:

[UIButton_G_obj addTarget:self action:@selector(buildUIWorkArea)
         forControlEvents:UIControlEventTouchUpInside];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM