3

他の投稿を検索しましたが、解決策が見つかりませんでした。

クリックできない UIButton があります。クリックしても暗くなりません。問題はセレクターメソッドに関するものではありません。

誰か助けてくれませんか?! コードは次のとおりです。

- (void)drawRect:(CGRect)rect
{

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)];

    UIView *detailsView = [[UIView alloc] init];

// I have other components here and after the UIButton

    UIButton *btnOpenPDF = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnOpenPDF setBackgroundImage:[UIImage imageNamed:@"btnVisualizarPdf"] forState:UIControlStateNormal];
    [btnOpenPDF setFrame:CGRectMake(35, totalHeight, 249, 30)];
    btnOpenPDF.userInteractionEnabled = YES;
    [btnOpenPDF addTarget:self action:@selector(openPdf:) forControlEvents:UIControlEventTouchDown];

    [detailsView addSubview:btnOpenPDF];

    [scroll addSubview:detailsView];

    [self addSubview:scroll];

}

上記のコードは、メソッド -loadView() を介して UIViewController を介して呼び出される UIView 内にあります。

以下は openPdf メソッドです。

- (IBAction)openPdf:(id)sender{
// Creates an instance of a UIWebView
    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320,450)];

// Sets the scale of web content the first time it is displayed in a web view
    aWebView.scalesPageToFit = YES;
    [aWebView setDelegate:self];

//Create a URL object.
    NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://portalarp.com.br/portal/%@",[[[[[[[delegateFunctions products] objectAtIndex:[delegateFunctions selectedProductIndex]] objectForKey:@"ata"] objectForKey:@"nm_arquivo"] objectForKey:@"text"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByReplacingOccurrencesOfString:@"\t" withString:@""]]];

//URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];

//load the URL into the web view.
    [aWebView loadRequest:requestObj];

    [self addSubview:aWebView];
    [aWebView release];
}
4

2 に答える 2

7

drawRectは、このコードを配置する奇妙な場所です。これはビューを描画するために呼び出され、おそらく何度も呼び出される可能性があります。そこでビューを構築するべきではありません。たとえばdrawRect、ボタンをタッチしたときに呼び出される場合があります (再描画するため)。

これが UIView にある場合は、おそらくinitこのコードを配置するのに適した場所です。

于 2012-04-30T19:13:46.407 に答える
3

問題はscrollViewだと思います..タッチイベントを転送していません..次のことを試してください:

scroll.userInteractionEnabled = YES;
scroll.exclusiveTouch = YES;
于 2012-05-02T07:25:58.013 に答える