简体   繁体   中英

Connect to an IOS IBAction which is not at a “valid connection destination”

I have been spending days to make connection from a button on the storyboard to a new NSObject class in an IOS project. Methods I used in OS X no longer work in IOS.

I have no trouble connection the button to the IBAction on the original UIViewController class. According to the Apple documentation my new class is not a "valid connection destination".

To work around the issue I tried to call the method in the new class from the UIViewController class. I tried notification and delegate schemes, using the posts on Stack Overflow, but could not get them to work.

The closest I came linking to a +method in the target class, such as

+ (void) startToneGenerator
{
    NSLog(@"Arrived in startToneGenerator");  
//        [self startPlay:nil];
}

However, from inside that method I can not call the -startPlay in the same class. Because the -StartPlay class is part of an audio unit construct I can not change it to a +starPlay class without breaking the audio unit.

Can anyone point me to some documentation which describes what "valid connection destination" really means, and how to make the attempted connection a valid one.


I used the method suggested in answer 1 in OS X, where it does work, but it no longer works in IOS. I am restating the code you clarify the problem.

// code in VC

- (IBAction)RunPauseStop:(id)sender
  {                
   NSLog(@"arrived in RunPauseSTop");   // OK

   [uToneGenerator testMethod]; // OK                 
   //  [uToneGenerator startPlay:self]; // crashes
}


// code in uToneGenerator

+ (void) testMethod
{
NSLog(@"arrived in <testMethod>");   // OK  
//  [uToneGenerator startPlay:nil];  // crashes
}


- (IBAction)startPlay:(id)sender
{
NSLog(@"arrived in <startPlay>"); // Don't get here           
}

The line [uToneGenerator startPlay:nil]; compiles (with a warning) but crashes with:

2012-03-06 13:20:25.509 Tinnitus Tamer IP[863:f803] +[uToneGenerator startPlay:]: unrecognized selector sent to class 0x6c40 2012-03-06 13:20:25.509 Tinnitus Tamer IP[863:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[uToneGenerator startPlay:]: unrecognized selector sent to class 0x6c40'

calling from testMethod; compiles but crashes with: 2012-03-06 13:22:31.413 Tinnitus Tamer IP[885:f803] +[uToneGenerator startPlay:]: unrecognized selector sent to class 0x6c44 2012-03-06 13:22:31.414 Tinnitus Tamer IP[885:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[uToneGenerator startPlay:]: unrecognized selector sent to class 0x6c44'

I noted that the error message can not find +[uToneGenerator startPlay:, but the method I am trying to call is -[uToneGenerator startPlay:


uToneGenerator *utg = [[uToneGenerator alloc]init]; works OK. In spite of the compiler warning "NSProject may not respond to 'startPlay'.

Thanks for helping me out. Much appreciated. I really was stuck.

You need to call whatever class method you want inside the IBAction method in your VC:

- (IBAction)myButtonPressed:(id)sender {
     [MyClass myClassMethod];
}

If you want that code to work you must declare - (IBAction)startPlay:(id)sender with a + . Otherwise you will have to make an instance of your class:

uToneGenerator *utg = [[uToneGenerator alloc]init]; //or whatever you use

and then use that to call your startPlay method:

[utg startPlay:self];

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM