Ios 以编程方式设置按钮标题
我在故事板中制作了一个按钮,并将其与头文件中的iAction关联。如何将此按钮的标题设置为我生成的变量Ios 以编程方式设置按钮标题,ios,uibutton,Ios,Uibutton,我在故事板中制作了一个按钮,并将其与头文件中的iAction关联。如何将此按钮的标题设置为我生成的变量displayPhone,并让它同时拨打该号码 .h #import <UIKit/UIKit.h> #import <Parse/Parse.h> @interface IBThirdViewController : UIViewController @property (nonatomic, strong) PFRelation *agentRelation; @
displayPhone
,并让它同时拨打该号码
.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface IBThirdViewController : UIViewController
@property (nonatomic, strong) PFRelation *agentRelation;
@property (nonatomic, strong) NSArray *agent;
@property (weak, nonatomic) IBOutlet UILabel *agentName;
@property (strong, nonatomic) IBOutlet UILabel *agentPhone;
@property (strong, nonatomic) IBOutlet UILabel *agentEmail;
@property (strong, nonatomic) IBOutlet UIImageView *agentImage;
- (IBAction)phoneButton:(id)sender;
@end
看起来您没有将按钮连接到插座,而只是连接到动作 首先,您需要将其连接到插座(就像您使用标签所做的那样) 然后使用
[self.button setTitle:@"blah" forState:UIViewControlStateNormal];
反正是这样的。我目前正在使用iPhone,因此没有自动完成功能。如果要更改iAction方法中的标题,请执行以下操作:
[sender setTitle:@"My title" forState:UIControlStateNormal];
如果您想在任何其他地方进行更改,您需要uibutton的@property,如下所示
@property (strong, nonatomic) IBOutlet UIButton *phoneButtonProperty;
然后把它连接到你的按钮上
[电话按钮属性集标题:@“我的标题”用于状态:uicontrol状态正常] 首先,您需要将xib中的连接按钮与
IBOutlet
对象连接:
@property (nonatomic, weak) IBOutlet UIButton *displayPhone;
然后在加载数据后,在-(void)viewdide:(BOOL)animated
中设置其标题:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//...
// Load data here
//...
[self.displayPhone setTitle:displayPhone forState:UIControlStateNormal];
}
最后对按钮执行iAction
方法:
- (IBAction)phoneButton:(id)sender {
NSString *phone = [sender titleForState:UIControlStateNormal];
// Remove all chars except of digits
static NSString *const kDigitsString = @"0123456789";
phone = [[phone componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:kDigitsString] invertedSet]] componentsJoinedByString:@""];
// Initiate call
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]]];
}
谢谢我是iOS新手,忘了我还必须添加一个插座。现在唯一的问题是,当我按下数字(555)555-5555时,什么也没有发生。@Packy我已经用删除所有非数字字符的代码更新了我的答案。现在,当你点击按钮时,iOS应该开始呼叫。谢谢。我刚刚完成了我的代码来做这件事,我突然想到这可能就是问题所在。
- (IBAction)phoneButton:(id)sender {
NSString *phone = [sender titleForState:UIControlStateNormal];
// Remove all chars except of digits
static NSString *const kDigitsString = @"0123456789";
phone = [[phone componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:kDigitsString] invertedSet]] componentsJoinedByString:@""];
// Initiate call
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]]];
}