hmlasnk
hmlasnk

Reputation: 1322

When addSubview and release not working properly in added view

I'm created Class and it's add to the current view with non arc project. After that i'm releasing it as this.

  TestViewController *tView=[[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , tView.view.frame.size.height);
[self.view addSubview:tView.view];
[tView release];

I added button to the TestViewController and when pressed it just crash and view this message from console.

-[TestViewController performSelector:withObject:withObject:]: message sent to deallocated instance 

Anyone can explain the reason for that?

Upvotes: 0

Views: 363

Answers (4)

qiyu
qiyu

Reputation: 71

Obviously, the target of your button is tView. [tView dealloc] is called after [tView release] as its retainCount decrease to 0. You should declare tView as a private member variable, such as _tView, and call [_tView release] in your view controller's dealloc function.

@interface **
{
    TestViewController *_tView;
}

if(!_tView){
    _tView=[[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
}
_tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , _tView.view.frame.size.height);
[self.view addSubview:_tView.view];

In iOS 5.*, custom container view controllers is supported. (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html ) You can write code like this:

TestViewController *tView=[[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , tView.view.frame.size.height);
[self.view addSubview:tView.view];
[self addChildViewController:tView];
[tView didMoveToParentViewController:self];
[tView release];

Upvotes: 0

Sonu
Sonu

Reputation: 248

you can use below code

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.yourViewController addSubview:button];

self.viewController means you have define your viewcontroller in .h file and then use you instance of the view controller to add your button.

then you can release your viewController [ViewController Release];

Upvotes: -1

Bhanu Prakash
Bhanu Prakash

Reputation: 1483

Currently, you had declared TestViewController instance as local. So, that only it is getting crash while accessing the controls which are there in the instance.

Declare TestViewController instance in class level(ivar) and then use it.

Upvotes: 0

Divyu
Divyu

Reputation: 1313

When you call [tView release]; TestViewController's dealloc method will automatically get called. And Objects of this class will be released. So Probably you have released that button in dealloc. That's why your app is crashing.

This is not the right way to do it. You should create a custom view and add that view to self.view instead of adding viewcontroller's view.

Upvotes: 2

Related Questions