【问题标题】:Can't start a call when a button in a UITableViewCell is pressed按下 UITableViewCell 中的按钮时无法开始通话
【发布时间】:2011-05-19 12:34:43
【问题描述】:

我的表格视图中的每个单元格都有一个按钮。我想在按下按钮时拨打一个号码。我以前使用过这样的代码调用:

NSString *cifra1=@"tel://";
NSString *cifra=customer.tel;     
cifra1 = [cifra1 stringByAppendingString:cifra];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:cifra1]];

如果我将此代码放在方法“didSelectRowAtIndexPath”中,一切正常。但如果我把它放在-(IBAction)buttonPressed:(UIButton *) sender 中,什么都不会发生。我用 NSLog 检查了这个方法,它响应按钮点击。

怎么了?

编辑:奇怪,这行得通:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


NSString *cifra1=@"tel://";
NSString *cifra=@"123456789";
cifra1 = [cifra1 stringByAppendingString:cifra];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:cifra1]];

}

尽管客户编号被正确读取,但这并没有:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

Customer *c=[customerArray objectAtIndex:indexPath.row];

NSString *cifra1=@"tel://";
NSString *cifra=c.tel;
cifra1 = [cifra1 stringByAppendingString:cifra];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:cifra1]];

}

【问题讨论】:

  • 您如何获得电话号码,即customer.tel。这是否因不同的行而异?如果是这样,如何从-buttonPressed: 方法中引用它。
  • 你能显示 didSelectRowAtIndexPath 的完整代码,以便我能说出 (IBAction)buttonPressed:(UIButton *) sender 有什么问题
  • @Deepak:当按下按钮并且一切正常时,我从 custoemr.tel 记录了号码,我得到了正确的号码。
  • 你能发布这两种方法吗?它以一种方法而不是另一种方法工作似乎很奇怪。

标签: iphone uitableview uibutton call


【解决方案1】:

是你的 buttonPressed 调用的吗?

尝试添加NSLog(@"%s", __FUNCTION__);

尝试使用 id 代替 UIButton:

-(IBAction)buttonPressed:(id) sender

【讨论】:

    【解决方案2】:

    (IBAction)buttonPressed:(UIButton *) 发送者

    将其更改为 (IBAction)buttonPressed:(id) 发件人

    【讨论】:

      【解决方案3】:

      确保使用addTarget:action:withObject:forControlEvents: 设置按钮的目标

      【讨论】:

      • buttonPressed 调用成功,问题不在这里。
      【解决方案4】:

      这样做

      NSString *cifra = [[c.tel copy] autorelease];
      

      我怀疑你在使用核心数据。

      【讨论】:

      • 这没有帮助,我没有使用核心数据。仅使用 NSString *cifra=@"123456789" 制作字符串文字才有效。我不明白。
      • cifra1openURL: 之前的值是多少?您可以通过NSLog 查询吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 2017-11-21
      • 2019-02-18
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多