Ios UIButton中的按钮标题意外更改

Ios UIButton中的按钮标题意外更改,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我用以下代码创建了一个ui按钮: applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)]; [applyButton titleColorForState:UIControlStateNormal]; [applyButton setTitle:@"MyTitle" forState:UIControlStateNormal]; applyButton.titleLabel.textColor=[UI

我用以下代码创建了一个
ui按钮

applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:@"MyTitle" forState:UIControlStateNormal];
 applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];

applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:@selector(saveSettingsToNSUser:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];
按钮最初以浅灰色显示“MyTitle”。
但当我按下它时,标题颜色变为白色,并调用“saveSettingsToNSUser”函数。函数“saveSettingsToNSUser”中没有影响按钮的内容。我不明白发生了什么事。我遗漏了什么吗?

您应该使用
设置标题颜色:forState:
来设置标题标签的颜色

applyButton= [[UIButton alloc] initWithFrame:CGRectMake(75, 636, 150, 50)];
//set title
[applyButton setTitle:@"MyTitle" forState:UIControlStateHighlighted];
[applyButton titleColorForState:UIControlStateNormal];
[applyButton setTitle:@"MyTitle" forState:UIControlStateNormal];
 applyButton.titleLabel.textColor=[UIColor lightGrayColor];
[[applyButton layer] setBorderWidth:2.0f];
[[applyButton layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];

applyButton.layer.cornerRadius = 10;
[applyButton addTarget:self action:@selector(saveSettingsToNSUser:)   forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:applyButton];
[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];

您应该使用
setTitleColor:forState:
设置标题标签颜色

[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];

您应该使用
setTitleColor:forState:
设置标题标签颜色

[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];

您应该使用
setTitleColor:forState:
设置标题标签颜色

[applyButton setTitleColor: [UIColor lightGrayColor] forState: UIControlStateNormal];