English 中文(简体)
iOS 程序创建按钮不工作
原标题:iOS Programmatically Created Button not Working

Hi 我有一个 < code> UI Button 程序上创建的 < code>, 但不幸的是它不起作用 。 当我点击 < code> UI Button 时, 任何事情都不会发生。 我不知道有什么问题。 任何帮助都会受到欢迎 。

UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor]; 
[connectedStories setTitle:@"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];

[viewContainer addSubview:contentView];
return contentView;

- (void)buttonClicked
{
  NSLog(@"button clicked");
}
最佳回答

I think labels recives touches instead of button. Add userInteractionEnabled = NO; to your labels.

label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
问题回答

尝试将代码的顶部部分修改为此 :

CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];

//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]]; 
[connectedStories setTitle:@"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];

很可能有以下几个方面:

[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];

确信你需要按以下顺序添加:

[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];

这是因为当您调用 addSubview: 时,它将它添加到子视图接收器列表的末尾,而最新的子View则在上方出现。

查看UIView Documents 这里

之所以没有检测到“互动”的原因 在于您没有通过触碰时间通过另一个子观察点, 因为它们被标签1 捕获并停留在那里。

默认情况下, 上一个子View 将记录所有触摸事件, 但不传递它们 。

选择器中选择器中您方法中最后一个方法中添加的冒号

[connectedStories addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
                 action:@selector(aMethod:)
       forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f  alpha:1];
 [custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
 [view addSubview:custombutton];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签