开发者

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 addS开发者_开发知识库ubview: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”.


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜