2012-05-05 42 views
2

我想爲我自己的委託方法,我在forDelegate.h文件中聲明'Mydelegate'協議。如何製作自己的委託方法?

@protocol Mydelegate <NSObject> 

-(void)show:(NSString *)msg; 

@end 

然後在我的第一個的viewController我做財產申報this.I的方法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

第一件事你打電話給你的演出方法我在無效的情況下,你應該使用一些地方。 – vishiphone

+0

我將編輯您的代碼並在下面發佈,只需查看它。 – vishiphone

+0

在SecondViewController中是否定義了「 - (void)show:(NSString *)msg」?並且您還在SecondViewController中使用了delegate1。 –

回答

0

這將是您的問題的useful答案。

+0

由於博客和鏈接隨着時間的推移可能會改變或消失,因此在回答這樣的問題時,應該從鏈接中包含相關信息,以便答案永遠在這裏。 :) – lnafziger

0

在視圖控制器的實現,你可以調用該委託的方法..

-(IBAction)clickMe:(id)sender 
{ 
    [self.delegate1 show:@"Your String Param"]; 
} 
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. 
    } 
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]; 
}