Aniruddh
Aniruddh

Reputation: 7668

Calling Delegate Method not Working

I have a UIButton which calls a method from another UIViewController, that method calls a delegate method which is not happening.

Something like:

FirstViewController

-(void)viewWillAppear:(BOOL)animated {
//Submit Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self 
               action:@selector(someMethod)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Submit" forState:UIControlStateNormal];
    button.frame = CGRectMake(100.0, 120.0, 80.0, 40.0);
    [self addSubview:button];
}

- (void)someMethod {

 SecondViewController *viewC = [[SecondViewController alloc] init];
 [viewC otherMethod];

} 

SecondViewController

- (void)otherMethod {

 NSLog(@"Verification.....");
 [self.delegate foo:self Bar:self.text]; //not working when called the otherMethod from FirstViewController

} 

I know I'm doing it wrong way.

Upvotes: 1

Views: 5012

Answers (3)

zahreelay
zahreelay

Reputation: 1742

A very simple example of the way protocols and delegates work in objective-c

@class A;
@protocol DelegateName<NSObject>
@optional
  - (void)doSomething:(NSString *)text;
@end

@interface A: NSObject {
    NSString *bar;
    id <DelegateName> delegate;
}

@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <DelegateName> delegate;

- (void)someAction;

@end

This is your protocol declaration. Then when you are calling the delegate method, you need an object of the class to invoke the delegate method. In your case it is vc. You set delegate like this:

[vc setdelegate: self];

Then you invoke method, like you have done.

See if you are missing any point from this example.

Upvotes: 3

trojanfoe
trojanfoe

Reputation: 122381

It is customary to validate that the delegate responds to the method you want to call, so your otherMethod method should be implemented like this, adding more logging to help debug:

- (void)otherMethod
{
    NSLog(@"delegate = %p", self.delegate);

    if ([self.delegate respondsToSelector:@selector(foo::)])
    {
        NSLog(@"Calling delegate foo");
        [self.delegate foo:self Bar:self.text];
    }
    else
    {
        NSLog(@"Delegate doesn't respond to foo");
    }
} 

Upvotes: 0

Dream.In.Code
Dream.In.Code

Reputation: 399

Try to understand how delegate methods work .

@protocol CPUPerformanceDelegate <NSObject>

-(void)getUsedCpuOperations:(float)percent;
-(void)getKernelCpuOperations:(float)percent;
-(void)getIdleCpuOperations:(float)percent;
-(void)getUserCpuOperations:(float)percent;
-(void)getNiceCpuOperations:(float)percent;

@end

@interface CPUPerformance : NSObject{

    processor_info_array_t          cpuInfo, prevCpuInfo;
    mach_msg_type_number_t          numCpuInfo, numPrevCpuInfo;
    unsigned                        numCPUs;

    NSLock                          *CPUUsageLock;



}
@property(nonatomic,assign)id<CPUPerformanceDelegate>delegate;
@property(nonatomic,retain)NSTimer                         *updateTimer;
@end

Then

#import "CPUPerformance.h"



@implementation CPUPerformance
@synthesize delegate,updateTimer;
- (void)updateInfo
{
idlepercent = ((idle/total)*100);
                userpercent = (user/total)*100;
                syspercent = (sys/total)*100;
                nicepercent = (nice/total)*100;
                inUsepercent = (inUse/total)*100;
[delegate getIdleCpuOperations:idlepercent];
            [delegate getKernelCpuOperations:syspercent];
            [delegate getNiceCpuOperations:nicepercent];
            [delegate getUsedCpuOperations:inUsepercent];
            [delegate getUserCpuOperations:userpercent];
}

and finally

#import "CPUPerformance.h"
@interface SpecProjectFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CPUPerformanceDelegate>{

    //Ivars

    NSMutableArray                  *processArray;
    //User Interface Object
    UILabel                         *cpuUsage;
    UILabel                         *cpuIdle;
    UILabel                         *cpuUser;
    UILabel                         *cpuNice;
    UILabel                         *cpuKernel;
    IBOutlet UITableView            *tableViews;
    CPUPerformance                  *cpuObject;

}
=================

#import "SpecProjectFirstViewController.h"


@implementation SpecProjectFirstViewController

-(void)getIdleCpuOperations:(float)percent{

     [cpuIdle setText:nil];

      [cpuIdle setText:[NSString stringWithFormat:@"Idle :%.0f %%",percent]];
     [cpuIdle setTextAlignment:UITextAlignmentCenter];
}

-(void)getKernelCpuOperations:(float)percent{
    [cpuKernel setText:nil];

    [cpuKernel setText:[NSString stringWithFormat:@"Kernel :%.0f %%",percent]];
    [cpuKernel setTextAlignment:UITextAlignmentCenter];
}


-(void)getNiceCpuOperations:(float)percent{
     [cpuNice setText:nil];

    [cpuNice setText:[NSString stringWithFormat:@"Nice :%.0f %%",percent]];
    [cpuNice setTextAlignment:UITextAlignmentCenter];
}

-(void)getUsedCpuOperations:(float)percent{

    [cpuUsage setText:nil];
    [cpuUsage setText:[NSString stringWithFormat:@"Used :%.0f %%",percent]];
    [cpuUsage setTextAlignment:UITextAlignmentCenter];
}

Upvotes: 1

Related Questions