开发者

UIButton addTarget method can not use in IOS 4.2

i have a UIButton made programmatically and i want to add the target and the action for that button. i have use method addTarget: action: forControlEvents:

in IOS 4.1 this method is detected, but in 4.2 it didn't, here is my code

UIButton *moreButton = [[[UIButton alloc] init] autorelease];

float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
    if(version <= 4.1){
        moreButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(6.3+5*widthSegment, 0.0, widthSegment, heightSegment)];
        [moreButton addTarget:self action:@sel开发者_StackOverflow社区ector(getPopOverMore:) forControlEvents:UIControlEventTouchUpInside];
    }
    else{
        //version 4.2
        NSLog(@"versi 4.2");
        moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        moreButton.frame = CGRectMake(6.3+7*widthSegment, 0.0, widthSegment, heightSegment);
        [moreButton addTarget:self action:@selector(getPopOverMore:) forControlEvents:UIControlEventTouchUpInside];
    }

and this is the action method :

- (IBAction)getPopOverMore:(id)sender{

if(moreFileController == nil) {

    moreFileController = [[MoreFilePopController alloc]
                          initWithStyle:UITableViewStylePlain];

    moreFileController.delegate = self;

    moreFilePopOverController = [[UIPopoverController alloc]
                                 initWithContentViewController:moreFileController];
}

CGRect frameMore = CGRectMake(6.3+5*widthSegment, 0.0, widthSegment, heightSegment);


[moreFilePopOverController presentPopoverFromRect:frameMore inView:navBar 
                         permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}

any body know, what's wrong here??


You don't need to put IBAction on UIButton sets manually.

[moreButton addTarget:self action:@selector(getPopOverMore:)       
 forControlEvents:UIControlEventTouchUpInside];

You need to declare in your file.m:

-(void)getPopOverMore:(id)sender{
}


It should work. also the initWithFrame for <=4.1 is unnecessary. i hope you are adding the button as a subview somewhere after this code.

try placing a breakpoint inside- (IBAction)getPopOverMore:(id)sender{

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜