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

时间:2016-06-23 16:40:56

标签: ios objective-c

这是接听来电和原始代码的代码 自动接听电话。

我正在更改它,以便在检测到来电时,答案 出现按钮,该按钮有效([_answerButton setAlpha:1];),但是 然后我想通过点击该按钮接受连接。我试过了 只是搬家:

    [connection accept];
    _connection = connection;

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

- (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;
    }
}

1 个答案:

答案 0 :(得分:1)

"我试着移动[连接接受]; _connection = connection;对于answerCall方法,但由于某些原因,它没有起作用。" - 如果这导致您的-answerCall:方法如下所示:

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

然后可以理解它没有用。在上面的代码中,connection方法尚未在-answerCall方法的范围内声明-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

Mage_Sales_Model_Service_Quote