tags:

views:

722

answers:

5

Hi Friends,

I have created a UIView and added label to it and latter assign it to Controller. Now whenever I click on my View it shows me "EXC_BAD_ACCESS”.

Below is my code.

//create a UIView in App Delegate  
UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];  

//created a Button and added to UIView  
UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
btnPointer.frame = cgframe; // provides both a position and a size  
[btnPointer setTitle:btnLabelText forState:UIControlStateNormal];  
[btnPointer addTarget:self action:@selector(generate:) forControlEvents:UIControlEventTouchUpInside];
[viewPtr addSubview:btnPointer];  

//Now need to add this UIView to a controller  
viewController.view = viewPtr;  

I am able to display the button on the Form but when I click on the form or the button I get "EXC_BAD_ACCESS”.

A: 

What is self in [btnPointer addTarget:self ... ? Is generate: called? Have you tried debugging there?

Adam Woś
+1  A: 

Hi Ekra,

Instead of use the autorelease

UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];

Create the UIView *viewPtr on the .h file, and only do the release in the dealloc method

So, you will have the declaration on your .h file


UIView *viewPtr;

And you .m file will be with the following lines, when you instantiate the viewPtr in the same place where you was doing previously, but without the autorelease, and the release on the dealloc method as follow:


viewPtr = [[UIView alloc] initWithFrame:frmRect];
.
.
.
.
.
.
.
.

- (void)dealloc {
    [viewPtr release];
    [super dealloc];
}

The autorelease is the main problem in your code, because when you do this, your UIView won't respond to any event.

Cheers,
VFN

vfn
viewController.view = viewPtr; - this line retains viewPtr, so don't think autorelease causes the problem here
Vladimir
Try to don't do the autorelease. you will see that the problem is there.
vfn
only to test, try to remove the autorelease, and let the view with no release calling, only for test. if it works you do what I have suggested on my answer
vfn
A: 

You should create the view in the .m file of your view controller like this.

    - (void)loadView
    {
        UIView *viewPtr = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //created a Button and added to UIView  
    UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    btnPointer.frame = cgframe; // provides both a position and a size  
    [btnPointer setTitle:btnLabelText forState:UIControlStateNormal];  
    [btnPointer addTarget:self action:@selector(generate:) forControlEvents:UIControlEventTouchUpInside];
    [viewPtr addSubview:btnPointer];  

    //Now need to add this UIView to a controller  
    self.view = viewPtr;
[viewPtr release];
    }

Hope this helps.

Thanks,

Madhup

Madhup
A: 

Thanks everybody for your reply. VFN :-> You were right the release part was doing some problem. The control was getting released..

Now its working fine.

Ekra
Please accept the correct answer (the green tick) rather than adding new answers.
Stephen Darlington
A: 

As vfn says, this you get the error because you release the view too early. The question I would be asking is why is this case different?

Normally in an assignment like this:

viewController.view = viewPtr;

The viewPtr is retained and you would be correct to release it yourself. But look at the definition:

@property(nonatomic, retain) UIView *view

This means that any value is simply assigned and not automatically retained.

Stephen Darlington