我正在尝试为此创建自己的委托方法,我在forDelegate.h文件中声明了'Mydelegate'协议。
@protocol Mydelegate <NSObject>
-(void)show:(NSString *)msg;
@end
然后在我的第一个viewController中我为此创建了属性。我声明了一个方法clickMe,按下按钮时调用,该按钮出现在我的firstviewcontroller .xib文件中。
@class SecondViewController;
@interface ViewController : UIViewController
{
id <Mydelegate> delegate1;
SecondViewController *secondController;
}
@property (assign) id <Mydelegate> delegate1;
-(IBAction)clickMe:(id)sender;
@end
然后在我的第二个视图控制器中采用此协议并定义此协议的方法
SecondViewController.h文件
@class ViewController;
@interface SecondViewController : UIViewController <Mydelegate>
{
ViewController *viewController;
}
SecondViewController.m文件
-(void)show:(NSString *)msg
{
NSLog(@"in delegate mathod");
}
-(void)viewDidLoad
{
viewController =[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.delegate1=self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
程序正在运行,但Mydelegate方法'show'不是调用。
答案 0 :(得分:0)
这是您问题的useful 答案。
答案 1 :(得分:0)
在ViewController的实现中,你应该调用委托的方法..
-(IBAction)clickMe:(id)sender
{
[self.delegate1 show:@"Your String Param"];
}
答案 2 :(得分:0)
在第二个视图控制器xib上创建一个按钮,并将方法连接到该按钮,然后在单击该按钮后检查您的控制台。
@class ViewController;
@interface SecondViewController : UIViewController <Mydelegate>
{
ViewController *viewController;
}
-(IBAction)showmsg;
SecondViewController.m file
-(void)show:(NSString *)msg
{
NSLog(@"in delegate mathod");
}
-(IBAction)showmsg
{
[self show:];
}
-(void)viewDidLoad
{
viewController =[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.delegate1=self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
答案 3 :(得分:0)
@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];
}
-(void)getUserCpuOperations:(float)percent{
[cpuUser setText:nil];
[cpuUser setText:[NSString stringWithFormat:@"User :%.0f %%",percent]];
[cpuUser setTextAlignment:UITextAlignmentCenter];
}
-(void)viewDidAppear:(BOOL)animated{
cpuObject = [[CPUPerformance alloc]init];
cpuObject.delegate = self;
[self creatObjectsOnView];
[super viewDidAppear:animated];
}