Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 轻触后更改UIButton的文本或背景_Objective C_Ios_Uibutton - Fatal编程技术网

Objective c 轻触后更改UIButton的文本或背景

Objective c 轻触后更改UIButton的文本或背景,objective-c,ios,uibutton,Objective C,Ios,Uibutton,我尝试更改3个按钮(标题+背景)的外观,以及单击其中一个按钮的依赖项中文本视图的文本。(类似于选项卡式内容) 我已经在xib中创建了4个元素,并将它们与.h链接: IBOutlet UIButton *buttonTab1; IBOutlet UIButton *buttonTab2; IBOutlet UIButton *buttonTab3; IBOutlet UITextView *thisDescription; - (IBAction)show

我尝试更改3个按钮(标题+背景)的外观,以及单击其中一个按钮的依赖项中文本视图的文本。(类似于选项卡式内容)

我已经在xib中创建了4个元素,并将它们与.h链接:

   IBOutlet UIButton *buttonTab1;
    IBOutlet UIButton *buttonTab2;
    IBOutlet UIButton *buttonTab3;

    IBOutlet UITextView *thisDescription;

    - (IBAction)showTab1:(id)sender;
    - (IBAction)showTab2:(id)sender;
    - (IBAction)showTab3:(id)sender;

@property (strong, nonatomic) IBOutlet UIButton *buttonTab1;
@property (strong, nonatomic) IBOutlet UIButton *buttonTab2;
@property (strong, nonatomic) IBOutlet UIButton *buttonTab3;
在.m处,我尝试更改4个元素(这里是按钮1的代码):


TextViewElement的文本变化良好,但标题和背景与xib中的相同。

要在按钮上设置背景图像和标题,应使用方法和方法。 由于按钮根据状态可能有不同的标题或背景,因此直接设置图像或标签将不起作用

这将让您了解如何在代码中实现这一点:

[buttonTab1 setBackgroundImage:[UIImage imageNamed:@"content_tab1_on.png"] forState:UIControlStateNormal];
[buttonTab2 setBackgroundImage:[UIImage imageNamed:@"content_tab2.png"] forState:UIControlStateNormal];
[buttonTab3 setBackgroundImage:[UIImage imageNamed:@"content_tab3.png"] forState:UIControlStateNormal];
[buttonTab1 setTitle:@"Tab1on" forState:UIControlStateNormal];
[buttonTab2 setTitle:@"Tab2" forState:UIControlStateNormal];
[buttonTab3 setTitle:@"Tab3" forState:UIControlStateNormal];

关键是您不需要更改代码中的背景图像。您要做的是设置UIButton对象的(前景)图像。您将得到IB中定义的背景图像、以编程方式设置的文本和以编程方式设置的图像

原则上,这应该是可行的,您甚至不会注意到您的错误,但在UIButton上,您不能同时设置图像和文本。一个按钮有两个按钮中的一个,但不能同时显示这两个按钮,或者至少不能同时显示这两个按钮

解决方案:设置背景图像而不是图像

[buttonTab1 setBackgroundImage:[UIImage imageNamed:@"content_tab1_on.png"] forState:UIControlStateNormal];
[buttonTab2 setBackgroundImage:[UIImage imageNamed:@"content_tab2.png"] forState:UIControlStateNormal];
[buttonTab3 setBackgroundImage:[UIImage imageNamed:@"content_tab3.png"] forState:UIControlStateNormal];
[buttonTab1 setTitle:@"Tab1on" forState:UIControlStateNormal];
[buttonTab2 setTitle:@"Tab2" forState:UIControlStateNormal];
[buttonTab3 setTitle:@"Tab3" forState:UIControlStateNormal];