有故障的按钮会在按下前使电话通话

问题描述

| 该代码有效,但是应用程序无需按按钮即可调用字符串。当我到达应用程序的特定页面时,它会绕过所有内容(其他按钮,地图视图等)并拨打电话... 我使用的代码是:
   - (void)loadNo2 {
        UIButton *no2 = [UIButton buttonWithType:UIButtonTypeDetaildisclosure];
        [no2 setTitle:@\"2108642700\" forState:UIControlStatenormal];
        [no2 setTitleColor:[UIColor whiteColor] forState:UIControlStatenormal];
        [no2 setBackgroundColor:[UIColor blackColor]];
        [no2 setFrame:CGRectMake(84,260,152,31)];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"tel://1234567890\"]];
        [self addSubview:no2];
    }
有什么问题吗???     

解决方法

这行弹出对话框进行拨号
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"tel://1234567890\"]];
您没有将其添加为按钮操作,而只是在调用它。 将该行替换为:
  [no2 addTarget:self 
          action:@selector(onNo2Touch) 
forControlEvents:UIControlEventTouchUpInside];
并添加以下内容:
-(void) onNo2Touch:(id) sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"tel://1234567890\"]];
}
    ,看起来您已经从创建按钮的方法中调用了openURL。 您应该将呼叫拨到UIApplication sharedApp ...(即
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"tel://1234567890\"]];
) 在从操作(映射到按钮)调用的方法中。