2012-05-05 3 views
2

私はforDelegate.hファイルで 'Mydelegate'プロトコルを宣言するために私自身のデリゲートメソッドを作成しようとしています。デリゲートメソッドの作成方法

@protocol Mydelegate <NSObject> 

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

@end 

はその後、私はthis.Iのプロパティは、ボタンを押して上の呼び出しであるメソッドのclickMeを宣言作る私の最初のViewControllerに、そのボタンは私のfirstviewcontrollerの.xibファイル内に存在しています。次いで

@class SecondViewController; 

@interface ViewController : UIViewController 
{ 
    id <Mydelegate> delegate1; 

    SecondViewController *secondController; 
} 
@property (assign) id <Mydelegate> delegate1; 

-(IBAction)clickMe:(id)sender; 

@end 

と私は私の第2のビューコントローラでは、このプロトコルを採用し、このプロトコルの方法

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

あなたがあなたのshowメソッドを呼び出す場所あなたがどこで何かを使うべきです。 – vishiphone

+0

私はあなたのコードを編集し、ちょうどそれをチェックアウトの下に投稿します。 – vishiphone

+0

定義は " - (void)show:(NSString *)msg" SecondViewController内にありますか?また、SecondViewControllerでdelegate1を使用しました。 –

答えて

0

これはusefulあなたの質問の答えです。 ViewControllerの実装では

+0

ブログやリンクは時間の経過とともに変化したり消えたりすることがあるので、このような質問に答えるときは、リンクからの関連情報を含めて回答が常にここにくるようにする必要があります。 :) – lnafziger

0

、あなたは

-(IBAction)clickMe:(id)sender 
{ 
    [self.delegate1 show:@"Your String Param"]; 
} 
0

は、第2のビューコントローラの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]; 
} 
関連する問題