UIButton addTarget方法不能在IOS 4.2中使用

时间:2010-12-24 04:35:43

标签: ipad ios4 uibutton target

我有一个以编程方式制作的UIButton,我想为该按钮添加目标和操作。我使用方法addTarget: action: forControlEvents:

在IOS 4.1中检测到此方法,但在4.2中没有检测到,这是我的代码

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:@selector(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];
    }

这是行动方法:

- (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];

}

任何人都知道,这里有什么不对吗?

2 个答案:

答案 0 :(得分:2)

您无需手动将IBAction设置为UIButton

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

You need to declare in your file.m:

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

答案 1 :(得分:0)

它应该工作。另外,&lt; = 4.1的initWithFrame也是不必要的。我希望你在这段代码之后的某个地方添加按钮作为子视图。

尝试在- (IBAction)getPopOverMore:(id)sender{

中放置一个断点