gpt4 book ai didi

ios - 如何在 Objective-C 中将 bool 值从一种方法发送到另一种方法

转载 作者:行者123 更新时间:2023-11-29 11:58:41 25 4
gpt4 key购买 nike

这是接听来电的代码,原来的代码只是自动接听电话。

我正在更改它,以便在检测到来电时,接听电话按钮出现,它有效(使用 [_answerButton setAlpha:1];),但是然后我想通过单击该按钮接受连接。我试过只是移动:

    [connection accept];
_connection = connection;

到 answerCall 方法,但由于某些原因不起作用。所以现在我只想添加一些代码,在我单击时将 BOOL 设置为 true答案按钮并将该 BOOL 传递回 didAnswerCall 并添加一个else if 该方法将在连接为真时接受连接。

- (IBAction)answerCall:(id)sender {
// Need code to run didReceiveIncomingConnection method again,
// but pass Bool for didAnswerCall that equals true
}

- (void)device:(TCDevice *)device
didReceiveIncomingConnection:(TCConnection *)connection
{
NSLog(@"Incoming connection from: %@", [connection parameters][@"From"]);
if (device.state == TCDeviceStateBusy) {
[connection reject];
}
// Need else if code in here that says if didAnswerCall is true,
then [connection accept]
else {
[_answerButton setAlpha:1];

//[connection accept];
//_connection = connection;
}
}

最佳答案

“我只是尝试将 [connection accept]; _connection = connection; 移动到 answerCall 方法,但由于某种原因没有奏效。” - 如果这导致您的 -answerCall: 方法如下所示:

- (IBAction)answerCall:(id)sender {
[connection accept];
_connection = connection;
}

那么没用也是可以理解的。在上面的代码中,connection 没有在-answerCall 方法的范围内声明。在其上调用方法没有任何意义。事实上,这不应该编译。

我不认为在您的类(class)中传递 bool 值是您最好的选择。通过尝试在 -answerCall 中接受连接,您有望成功。您所需要的只是让您的类持有对 connection 的引用。在您的 @interface 中尝试添加一个属性,以便您的类的实例可以记住 connection:

@interface YourClass()

@property (nonatomic) TCConnection *connection;

// other code ...

@end

@implementation YourClass

- (IBAction)answerCall:(id)sender {
if (self.connection) {
[self.connection accept];
}
}

- (void)device:(TCDevice *)device
didReceiveIncomingConnection:(TCConnection *)connection {
NSLog(@"Incoming connection from: %@", [connection parameters][@"From"]);
if (device.state == TCDeviceStateBusy) {
[connection reject];
}
else {
// remember the connection here
self.connection = connection;
[_answerButton setAlpha:1];
}
}

// other code ...

@end

关于ios - 如何在 Objective-C 中将 bool 值从一种方法发送到另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com