xcode 使用标签作为按钮 - 触摸标签时,为字符串赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15865277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:09:01  来源:igfitidea点击:

Using a label as a button - when label is touched, assign value to a string

xcodebuttonuibuttonuilabel

提问by user1362308

I am having some challenges with using a label as a button. I have a label defined as a UILabel, and declared the string variable in my .h file:

我在使用标签作为按钮时遇到了一些挑战。我有一个定义为 UILabel 的标签,并在我的 .h 文件中声明了字符串变量:

IBOutlet UILabel *l1;

@property(strong)NSString *myvariable;

In the xib, with the label selected, in the Identity Inspector view, under Document, the Label field with the desired label name is set - example: l1. I also have some static text in the label.

在 xib 中,选择标签后,在 Identity Inspector 视图中的 Document 下,设置具有所需标签名称的 Label 字段 - 例如:l1。我在标签中还有一些静态文本。

In File's Owner Connection Inspector, I have the l1 Outlet connected to the l1 label.

在 File's Owner Connection Inspector 中,我将 l1 插座连接到 l1 标签。

My goal: When the label is touched, I would like to simply assign a string variable with a text value such as what might be done in the .m file:

我的目标:当标签被触摸时,我想简单地分配一个带有文本值的字符串变量,例如可能在 .m 文件中执行的操作:

self.myvariable = l1.text;

Things that I have unsuccessfully tried: - setting up a UIButton in my .h, - tried to set up a UIButton in the .m - in the XIB Identity Inspector view, under Accessibility, tried checking the button Traits checkbox to enable the label as a button

我尝试过失败的事情: - 在我的 .h 中设置一个 UIButton, - 尝试在 .m 中设置一个 UIButton - 在 XIB Identity Inspector 视图中的辅助功能下,尝试选中按钮 Traits 复选框以启用标签一个按钮

I haven't figured it out yet. This is probably an easy label question, and most of my experience with labels include static text display, or displaying a countdown based on present date using epoch time. Any guidance would be greatly appreciated!

我还没有弄清楚。这可能是一个简单的标签问题,我对标签的大部分经验包括静态文本显示,或使用纪元时间显示基于当前日期的倒计时。任何指导将不胜感激!

Thanks in advance! Rob

提前致谢!抢

回答by matto0

Looking around, most people recommend using an actual button and setting the background transparent or to the background color.

环顾四周,大多数人建议使用实际按钮并将背景设置为透明或背景颜色。

That said, you can do something like:

也就是说,您可以执行以下操作:

In viewWillLoad:

在 viewWillLoad 中:

    UILabel * label = [[UILabel alloc] init]; // change/delete to use your obj

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAction)];
[label addGestureRecognizer:tap];
label.userInteractionEnabled = YES;

then set the selector:

然后设置选择器:

- (void) pushAction
{
    self.myvariable = l1.text;
}

回答by ssantos

You could create a custom class extending UILabel, which overrides

您可以创建一个自定义类扩展UILabel,它覆盖

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

You may assign your new custom UILabel in xib's identity inspector.

您可以在 xib 的身份检查器中分配新的自定义 UILabel。

回答by sunil jangir

UILabel *lblname        = [[ UILabel alloc]initWithFrame: CGRectMake(10, 120, 200, 35)];
lblname.text            = @"sunil";
lblname.textColor       = [UIColor blueColor];
lblname.textColor       = [UIColor blueColor];
lblname.backgroundColor = [UIColor orangeColor];
[self.view addSubview:lblname];

UIButton *btnpress      = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnpress.frame          = CGRectMake(10, 180, 300, 20);
[btnpress setTitle:@"click" forState:UIControlStateNormal];
[btnpress addTarget:self action:@selector(btnpress1) forControlEvents:UIControlEventTouchUpInside];
btnpress.backgroundColor = [UIColor redColor];
[self.view addSubview:btnpress];

回答by young_souvlaki

I wanted to have my whole screen as a button that when pressed changes the text on a label that appears over the button. This is how I did it on xcode:

我想让我的整个屏幕作为一个按钮,按下时会更改显示在按钮上的标签上的文本。这就是我在 xcode 上的做法:

In the storyboard I dragged a button over to the interface. At the top right where you edit the button I set the button content to "group" instead of text. DO NOT set alpha to 0 on the button. I then dragged a label on top of the button/group. When ctrl-dragging over the necessary actions and outlets be sure to do it by exposing the left panel of the storyboard. It is likely you will only be able to access the group by mouse after changing the button content type.

在故事板中,我将一个按钮拖到界面上。在您编辑按钮的右上角,我将按钮内容设置为“组”而不是文本。不要在按钮上将 alpha 设置为 0。然后我在按钮/组的顶部拖动了一个标签。当按住 ctrl 拖动必要的动作和出口时,一定要通过暴露故事板的左侧面板来做到这一点。在更改按钮内容类型后,您可能只能通过鼠标访问该组。