objective-c - UIButton 不可点击

标签 objective-c ios cocoa-touch uibutton

我在其他帖子中搜索过,但找不到解决方案。

我有一个无法点击的 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];

}

上面的代码在 UIView 中,它通过 UIViewController 通过方法 -loadView() 调用。

下面是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];
}

最佳答案

drawRect 是放置此代码的奇怪位置。它被称为绘制 View ,并且可以想象被调用很多次——你不应该在那里构建 View 。例如,当您触摸按钮(重绘)时,可能会调用 drawRect

如果这是在 UIView 中,init 可能是放置此代码的更好位置。

关于objective-c - UIButton 不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389185/

相关文章:

iphone - 我的@synchronized block 出了什么问题?

objective-c - Objective-C ,如何使事件 View 出现在顶部通知菜单上

ios - <hr> 在 UITextView 中分离 [iOS]

ios - 在本地化字符串中使用变量

ios - Superview 忽略 UserInteractionEnabled = NO?

ios 检测到与其他 ios 设备的设备冲突,可能吗?

ios - 扩展 UIControl 命中区域的最佳方式?

ios - 在 UIView beginAnimations 完成后做一个 Action

ios - 是否可以在后台检测用户的移动事件?

ios - 为什么 UIFont(描述符 :size:) is x200 slower than UIFont(name:size:)?