调用委托方法不工作

问题描述:

我有一个UIButton调用另一个方法UIViewController,该方法调用委托方法,这是不发生。调用委托方法不工作

喜欢的东西:

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 

} 

我知道我这样做是错误的方式。

+0

可以提供多一点你的代码让我们看看有什么不对? – zahreelay

+1

如果将日志行更改为'NSLog(@“Verification .....%@”,self.delegate);'? –

+0

@PhillipMills我看到“Verification ......(null)”。 –

一个非常简单的方式协议和委托例如在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 

这是你的协议宣言。 然后,当您调用委托方法时,您需要该类的一个对象来调用委托方法。在你的情况下,它是vc。你设置这样的代表:

[vc setdelegate: self]; 

然后你调用方法,就像你所做的一样。

看看你是否缺少这个例子的任何一点。

+0

setdelegate定义在哪里?你必须写这个方法吗? – drlobo

+0

代表被设置为属性,所以你不必这样做。 – zahreelay

试着了解委托方法是如何工作的。

@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 

然后

#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]; 
} 

最后

#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]; 
} 
+0

我已经完成了。当我从FirstViewController调用otherMethod时,它不会调用它。 –

这是习惯,以验证委托回应要调用的方法,所以你otherMethod方法应该是这样来实现,增加更多的日志记录,以帮助调试:

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