0

I'm learning objective-c for iOS and have a question about creating my first target-action mechanism. I've got it to work, but currently I just set the target: portion of the addTarget:action:changeForControlEvents: method to nil, meaning it will search around my app for the target instead of drilling down on ViewController.m, where the method I want to send a message is located.

How can I tell the addTarget:action:changeForControlEvents: method which class to search first?

Here is a simple version of my current code:

The view:

// View.m
#import View.h

@implementation

- (void)sendAction
{
     UIControl *button = [[UIControl alloc] init];
     [button addTarget:nil  // How do I make this look for ViewController.m?
                action:@selector(changeButtonColor) 
changeforControlEvents:UIControlEventTouchUpInside];
}
@end

...and the View Controller:

// ViewController.m
#import ViewController.h

@implementation

- (void)target
{
     NSLog(@"Action received!");
}
@end

Thanks for the help!

rfarry
  • 42
  • 1
  • 7
  • 2
    One common approach is the delegate pattern, which is covered extensively. There's a good basic writeup [here](http://www.raywenderlich.com/46988/ios-design-patterns) or see the many extant Stack Overflow questions on this topic. – Aaron Brager Jul 01 '14 at 04:01
  • @rfarry here you can use the delegate methods... – Sunny Shah Jul 01 '14 at 04:31
  • You can able to point `changeButtonColor` of `ViewController.m` by mentioning the target as reference to `ViewController` class. – Shiva Kumar Ganthi Jul 01 '14 at 05:16

2 Answers2

0

Assuming ViewController is the VC that created the view you're working in you should be able to use:

addTarget:[self superview]

RegularExpression
  • 3,531
  • 2
  • 25
  • 36
  • Thanks, this works as well, but this is almost exactly what my code does implicitly (except it doesn't search in the current instance). I'm looking for a way to start the search in a specific instance. – rfarry Jul 01 '14 at 05:31
0

You cannot simply called UIViewController if it doesn't load or allocate in the memory. To achieve this you need to alloc that class.

one way to do using singleton

[button addTarget:[ViewController sharedManager] action:@selector(target) 
forControlEvents:UIControlEventTouchUpInside];

or using NSNotificationCenter, assuming that the class is already running (stack in previous navigation / another tabs).

// View.m
#import View.h
@implementation

- (void)sendAction
{
     UIControl *button = [[UIControl alloc] init];
     [button addTarget:self 
                action:@selector(controlAction) 
changeforControlEvents:UIControlEventTouchUpInside];
}

-(void)controlAction
{
 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"changeButtonColor" 
        object:self];
}
@end

and for target UIViewController

// ViewController.m
#import ViewController.h

@implementation
-(void) viewDidLoad
{
   [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveNotification:) 
        name:@"changeButtonColor"
        object:nil];

- (void)receiveNotification:(NSNotification *) notification

{
     NSLog(@"Action received!");
}
@end
Community
  • 1
  • 1
HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • I haven't tackled notifications yet, but don't most design patterns dictate that they are just for the model to broadcast information to the view controller and possibly the view? Thanks though, very helpful. – rfarry Jul 01 '14 at 05:22