我在故事板中创建了一个按钮,并将其与头文件中的IBAction相关联。如何将此按钮的标题设置为我创建的变量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
的.m
#import "IBThirdViewController.h"
#import "IBAgentsTableViewController.h"
@interface IBThirdViewController ()
@end
@implementation IBThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Find the Agent and show it
self.agentRelation = [[PFUser currentUser] objectForKey:@"agentRelation"];
PFQuery *query = [self.agentRelation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// success
self.agent = objects;
// Do something with the found objects
for (PFObject *object in objects) {
NSLog(@"name: %@", [object objectForKey:@"name"]);
NSLog(@"email: %@", [object objectForKey:@"email"]);
NSString *displayEmail = [object objectForKey:@"email"];
NSString *displayName = [object objectForKey:@"name"];
NSString *displayPhone = [object objectForKey:@"phone"];
PFFile *thumbnail = [object objectForKey:@"profilePic"];
NSURL *imageFileURL = [[NSURL alloc] initWithString:thumbnail.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileURL];
self.agentEmail.text = displayEmail;
self.agentName.text = displayName;
self.agentPhone.text = displayPhone;
self.agentImage.image = [UIImage imageWithData:imageData];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showEditAgent"]) {
IBAgentsTableViewController *viewController = (IBAgentsTableViewController *)segue.destinationViewController;
viewController.agents = [NSMutableArray arrayWithArray:self.agent];
}
}
- (IBAction)phoneButton:(id)sender {
}
@end
答案 0 :(得分:2)
首先,你需要xib中的连接按钮和IBOutlet
对象:
@property (nonatomic, weak) IBOutlet UIButton *displayPhone;
然后在加载数据后在-(void)viewDidAppear:(BOOL)animated
中设置它的标题:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//...
// Load data here
//...
[self.displayPhone setTitle:displayPhone forState:UIControlStateNormal];
}
最后为按钮实现IBAction
方法:
- (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]]];
}
答案 1 :(得分:1)
看起来您还没有将按钮连接到插座,只能连接到操作。
首先,您需要将其连接到插座(就像您使用标签一样)。
然后使用......
[self.button setTitle:@"blah" forState:UIViewControlStateNormal];
无论如何都是这样的。我目前在iPhone上,所以我没有自动完成。
答案 2 :(得分:0)
如果要更改IBAction方法内的标题,请执行以下操作:
[sender setTitle:@"My title" forState:UIControlStateNormal];
如果您想在任何其他地方进行更改,您需要@property of uibutton,类似这样的
@property (strong, nonatomic) IBOutlet UIButton *phoneButtonProperty;
并将其连接到您的按钮,然后
[phoneButtonProperty setTitle:@“我的标题”forState:UIControlStateNormal];