1

我正在尝试为此创建自己的委托方法,我在 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”。

4

4 回答 4

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];
}
于 2012-05-05T11:59:26.893 回答
0

这将是您问题的有用答案。

于 2012-05-05T05:28:34.360 回答
0

在 ViewController 的实现中,你应该调用委托的方法..

-(IBAction)clickMe:(id)sender
{
    [self.delegate1 show:@"Your String Param"];
}
于 2012-05-05T05:35:15.453 回答
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.
     }
于 2012-05-05T05:37:28.690 回答