0

First Controller's code:

 optionscontroller =  [[OptionsViewController alloc] init ];

 [optionscontroller setupSocket];

Second controller's code:

- (void)setupSocket
 {
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self      delegateQueue:dispatch_get_main_queue()];

NSError *error = nil;

if (![udpSocket bindToPort:0 error:&error])
{
    //  [self logError:FORMAT(@"Error binding: %@", error)];
    return;
}
if (![udpSocket beginReceiving:&error])
{
    //  [self logError:FORMAT(@"Error receiving: %@", error)];
    return;
}

isRunning = YES;
NSLog(@"Udp Echo server started on port %hu", [udpSocket localPort]); }

One of actions:

- (IBAction)testbutton:(id)sender {


NSString *host = @"192.168.1.255";
if ([host length] == 0)
{

    NSLog(@"Address required");

    return;
}


NSString *msg = @"hi";
if ([msg length] == 0)
{
    //[self logError:@"Message required"];
    return;
}

NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
[udpSocket enableBroadcast:YES error:nil ];
[udpSocket sendData:data toHost:host port:port withTimeout:-1 tag:0];



tag++;

}

When i call "setupSocket" from first controller it starts, but when i go to OptionsViewController and click the button - nothing happens, it's connoted right and it works, when i call "setupSocket" from second controller's "view did load function" everything works okay, why???

So if initialize some methods not from target view controller this methods doesn't work. i can't understand how it may happen?

Horhe Garcia
  • 882
  • 1
  • 13
  • 28
  • How do you go the the `OptionsViewController`? I am pretty sure you use a different instance than the one you used in `[optionscontroller setupSocket];`. Also, what do you mean by `action method doesn't work`? can you be more specific? – sch Apr 12 '12 at 18:03

1 Answers1

0

If you are using a XIB with interface builder you need to make sure that you wire the button up to it's action. Do this by right clicking and dragging from your button to your file's owner in the Interface Builder.

Oh also, you need to optionscontroller = [[OptionsViewController alloc] initWithNibName:@"YOURNIBNAMEHERE"];

If you are creating it programmatically (which if you're using IBAction you should not be) then you would use this:

addTarget:action:forControlEvents:

Read here:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html

Matt Hudson
  • 7,329
  • 5
  • 49
  • 66
  • If you are using a XIB file you will have to load it at some point. IBAction indicates you're using Interface Builder and a XIB file probably. – Matt Hudson Apr 12 '12 at 18:41