views:

680

answers:

3

Hi there, I have the following problem: A UIView with a UIViewController, but I have another view, something like an customized keyboard, and this "keyboard" have another UIViewController associated. I'm adding this keyboard to the original UIView like that

  CustomizedKeyboard *customized = [[CustomizedKeyboard alloc] initWithNibName:@"CustomizedKeyboard" bundle:[NSBundle mainBundle]];
    [self.view addSubview: customized.view];
    customized.view.frame =  CGRectMake(0, 480, 320, 260);    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    customized.view.frame =  CGRectMake(0, 200, 320, 260);
    [UIView commitAnimations];
    [customized release];

When I call this method, the view appears with no problem in my view, but the problem is that this customized keyboard have some methods associated with the buttons, but when I touch any button I got

-[NSCFType buttonPressed]: unrecognized selector sent to instance 0x1065f80'

Anyone have an idea what it might be? I create this sample project with this problem for better comprehension http://www.2shared.com/file/6174665/9c9bbd44/ArchiveFixed.html (Download link) I'm really grateful for any help. thx

+1  A: 

You’re releasing the view controller. Something needs to hang on to it.

Chris Suter
A: 

I see two things. First, Are you really using a custom bundle? I have generally seen nil passed here, especially for simple apps (as you describe this one). Second, do not add the subview until you are done with all the View's setup.

Try this:

CustomizedKeyboard *customized = [[CustomizedKeyboard alloc] initWithNibName:@"CustomizedKeyboard" bundle: nil];
customized.view.frame =  CGRectMake(0, 480, 320, 260);    
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
customized.view.frame =  CGRectMake(0, 200, 320, 260);
[UIView commitAnimations];
[self.view addSubview: customized.view];
[customized release];

You should be safe to release the customized instance as long as you successfully get it added to the parent's stack.

MystikSpiral
A: 

You should allways remember that, according to Apple when you use alloc, retain, copy .. you are responsible lifetime for this object. You code seems right but it should be slightly different, you are creating your object and relasing it, but you will need this object into your code.

It seems you are releasing your keyborad view controller instance, my suggestion that you should create class scope property and you can add keyboard view controller instance to this property before release it, you will need it.

I changed you example code like this.

for example

@synthisize customizedKeyboard;

CustomizedKeyboard *customized = [[CustomizedKeyboard alloc] initWithNibName:@"CustomizedKeyboard" bundle:[NSBundle mainBundle]];
self.customizedKeyboard = nil;
[self.customizedKeyboard release]; //prevent retain counts memory leaks
self.customizedKeyboard = customized;
[customized release];

[self.view addSubview: self.customizedKeyboard.view];
customized.view.frame =  CGRectMake(0, 480, 320, 260);    
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
customized.view.frame =  CGRectMake(0, 200, 320, 260);
[UIView commitAnimations];

It should be works with this changes.

fyasar