1

我有一个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

} 

我知道我做错了。

4

3 回答 3

3

协议和委托在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];

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

看看你是否错过了这个例子中的任何一点。

于 2012-05-12T14:32:14.370 回答
1

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

@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];
}
于 2012-05-12T14:17:50.600 回答
0

习惯上验证委托是否响应了您要调用的方法,因此您的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");
    }
} 
于 2012-05-12T14:39:46.823 回答