我有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 :(得分: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];
然后你调用方法,就像你做的那样。
看看你是否遗漏了这个例子中的任何一点。
答案 1 :(得分: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];
}
答案 2 :(得分: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");
}
}