Ekra
Ekra

Reputation: 3301

Create UIView through code and add events in iphone

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”.

Upvotes: 1

Views: 8091

Answers (4)

Stephen Darlington
Stephen Darlington

Reputation: 52575

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.

Upvotes: 0

vfn
vfn

Reputation: 6066

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

Upvotes: 1

Madhup Singh Yadav
Madhup Singh Yadav

Reputation: 8124

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

Upvotes: 1

Adam Woś
Adam Woś

Reputation: 2473

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

Upvotes: 0

Related Questions