IBAactions 不起作用
我有两个按钮没有执行它们被编程执行的 IBAction。我在 Interface Builder 中正确连接了它们,但是当我在模拟器或手机上单击它们时,它们不执行任务。没有崩溃,什么都没有。这是我的代码
MapDetailInfo.h:
@interface MapDetailInfo : UIViewController {
IBOutlet UILabel *name;
IBOutlet UITextView *address;
NSString * nameText;
NSString * addressText;
NSString * stringOne;
NSString * stringTwo;
IBOutlet UILabel *phoneNumber;
}
@property (retain, nonatomic) IBOutlet UILabel *name;
@property (retain, nonatomic) IBOutlet UITextView *address;
@property (nonatomic, copy) NSString * nameText;
@property (nonatomic, copy) NSString * addressText;
@property (retain, nonatomic) NSString * stringOne;
@property (retain, nonatomic) NSString * stringTwo;
@property (retain, nonatomic) IBOutlet UILabel *phoneNumber;
- (IBAction)callPhone:(id)sender;
- (IBAction)getDirections:(id)sender;
@end
MapDetailInfo.m:
@implementation MapDetailInfo
@synthesize name, address, nameText, addressText, phoneNumber, stringTwo, stringOne;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *myWords = [addressText componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"("]
];
stringOne = [myWords objectAtIndex:1];
stringTwo = @"(";
stringTwo = [stringTwo stringByAppendingString:stringOne];
self.name.text = nameText;
self.address.text = [myWords objectAtIndex:0];
self.phoneNumber.text = stringTwo;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(callPhone:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Call Shop" forState:UIControlStateNormal];
button.frame = CGRectMake(104.0, 332.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (IBAction)callPhone:(id)sender {
NSString *URLString = [@"tel://" stringByAppendingString:stringTwo];
NSURL *URL = [NSURL URLWithString:URLString];
[[UIApplication sharedApplication] openURL:URL];
}
- (IBAction)getDirections:(id)sender {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",self.address.text]]];
}
我从地图中的注释中提取此视图。一切正常,接受这两个按钮不执行其任务。任何帮助将不胜感激。谢谢!
I have two buttons that are not performing the IBAction that they are programmed to do. I have them correctly connected in Interface Builder, but when I click them in the simulator or on a phone, they do not perform their task. No crashes, nothing. Here is my code
MapDetailInfo.h:
@interface MapDetailInfo : UIViewController {
IBOutlet UILabel *name;
IBOutlet UITextView *address;
NSString * nameText;
NSString * addressText;
NSString * stringOne;
NSString * stringTwo;
IBOutlet UILabel *phoneNumber;
}
@property (retain, nonatomic) IBOutlet UILabel *name;
@property (retain, nonatomic) IBOutlet UITextView *address;
@property (nonatomic, copy) NSString * nameText;
@property (nonatomic, copy) NSString * addressText;
@property (retain, nonatomic) NSString * stringOne;
@property (retain, nonatomic) NSString * stringTwo;
@property (retain, nonatomic) IBOutlet UILabel *phoneNumber;
- (IBAction)callPhone:(id)sender;
- (IBAction)getDirections:(id)sender;
@end
MapDetailInfo.m:
@implementation MapDetailInfo
@synthesize name, address, nameText, addressText, phoneNumber, stringTwo, stringOne;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *myWords = [addressText componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"("]
];
stringOne = [myWords objectAtIndex:1];
stringTwo = @"(";
stringTwo = [stringTwo stringByAppendingString:stringOne];
self.name.text = nameText;
self.address.text = [myWords objectAtIndex:0];
self.phoneNumber.text = stringTwo;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(callPhone:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Call Shop" forState:UIControlStateNormal];
button.frame = CGRectMake(104.0, 332.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (IBAction)callPhone:(id)sender {
NSString *URLString = [@"tel://" stringByAppendingString:stringTwo];
NSURL *URL = [NSURL URLWithString:URLString];
[[UIApplication sharedApplication] openURL:URL];
}
- (IBAction)getDirections:(id)sender {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current Location&daddr=%@",self.address.text]]];
}
I am pulling this view from an annotation in a map. Everything works perfect accept for these two buttons not performing their task. Any help would be much appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按钮不起作用的原因是 URL 不正确。如果您遇到同样的问题,请确保您的电话号码 URL 是:
我的电话号码 URL 中有括号和破折号,这就是它无法显示的原因。我的谷歌网址也有错误。
谢谢!
The reason the buttons were not working is because the URL's were incorrect. If you run into the same problem, make sure your telephone number URL's are:
I had the parenthesis and dash inside of mine and that is why it would not pull up. Also there was an error in my google URL.
Thanks!
我在您的代码中没有看到任何
UIButton
。你用过它们吗?编辑:
我建议您使用按钮来实现特定目的。是的,您仍然可以使用标签并按照相同的过程来调用方法。
您连接插座的方式一定存在一些问题。
我建议您遵循此 Youtube 链接的教程 - 4、5 和 6。
http://www.youtube.com/watch?v=B7jB0D3Y7Ws&feature=relmfu
I dont see any
UIButton
in your code. Did you use them ?Edit:
I had suggested that you use buttons for that is the specific purpose. And yes, you can still use labels and follow the same procedure to call the methods.
There has to be some issue with the way you are connecting the outlets.
I would suggest that you follow tutorials - 4, 5, and 6 of this Youtube link.
http://www.youtube.com/watch?v=B7jB0D3Y7Ws&feature=relmfu