每次按下按钮都会更改标题 [英] Button changes title every time pressed

查看:29
本文介绍了每次按下按钮都会更改标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个,但是当我按下它时它不会改变,它只显示文本.也试过 if else 函数,但没有结果.

Im trying this, but when i press it it dont changes, it only displays text. Also tried if else functions, but with no results.

.h

IBOutlet UIButton *poga1;
@property (nonatomic, retain)  UIButton *poga1;

.m

@synthesize poga1;

- (void)viewDidLoad
{
[super viewDidLoad];

[poga1 setTitle:@"My Title" forState:UIControlStateNormal];
[poga1 setTitle:@"My Selected Title" forState:UIControlStateSelected];

}

推荐答案

试试这个:如果你通过故事板(gui界面)添加了一个标题,然后删除它.

Try this: If you added a title through the storyboard (the gui interface) then delete it.

.h//创建一个IBOutlet动作.如何?右键单击该按钮并将其拖到 .h 文件中.出现弹出窗口,您需要单击下拉菜单并将其切换为操作//应该是这样的

.h //create an IBOutlet action. How? You right-click the button and drag it to the .h file. A pop up happens and you need to click the drop down and switch it to action //should be something like

- (IBAction)yourButton:(id)sender;

.m
//there will be a new function in your .m which is your button action function. 
@synthesize poga1;

- (void)viewDidLoad
{
[super viewDidLoad];

[poga1 setTitle:@"My Title" forState:UIControlStateNormal];
[poga1 setTitle:@"My Selected Title" forState:UIControlStateSelected];

}

- (IBAction)Start:(id)sender
{

/* 如果您希望每次按下时都更改文本,那么您需要一个全局计数器,每次按下按钮时都会增加该计数器.那么您将有一个 if 语句,说明如果计数器等于 1(按一次),则将标题设置为 hello,如果计数器等于 7(按 7 次),则将标题设置为再见.*/

/* if you want this to change the text each time it's pressed then, you need a global counter that your increment each time the button is pressed. then you will have a if statement saying if the counter is equal to 1 (pressed once) then set title to hello, if the counter is equal to 7 (pressed 7 times) set title to goodbye. */

 [sender setTitle:@"whatever" forState:UIControlStateNormal];
}

这篇关于每次按下按钮都会更改标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆