David
David

Reputation: 95

How to send bool value from one method to another in Objective-C

This is code to answer an incoming call and the original code just answered the call automatically.

I am changing it so that when an incoming call is detected, an Answer button appears, which works (with [_answerButton setAlpha:1];), but then I want to accept the connection by clicking that button. I tried just moving:

    [connection accept];
    _connection = connection;

to the answerCall method, but for some reason that didn't work. So now I just want to add some code that will set a BOOL to true when I click the answer button and pass that BOOL back to didAnswerCall and add an else if to that method that will accept the connection when it's true.

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

Upvotes: 1

Views: 171

Answers (1)

Phillip Martin
Phillip Martin

Reputation: 1960

"I tried just moving [connection accept]; _connection = connection; to the answerCall method, but for some reason that didn't work." - If this caused your -answerCall: method to look like the following:

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

Then it is understandable that it didn't work. In the above code, connection has not been declared in the scope of the -answerCall method. Calling a method on it doesn't make any sense. In fact, this shouldn't compile.

I don't believe passing a boolean around your class is your best bet. You were on track to succeed by trying to accept the connection in -answerCall. All you need is to have your class hold a reference to connection. In your @interface try adding a property so that an instance of your class can remember 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

Upvotes: 1

Related Questions