2012-05-12 32 views
1

UIViewControllerのメソッドを呼び出すUIButtonがあり、そのメソッドはデリゲートメソッドを呼び出していません。私はそれを間違った方法をやって知っデリゲートメソッドが正常に動作しない

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

あなたはもう少しを提供することができます私たちに何が間違っているかを見せるためにあなたのコードの? – zahreelay

+1

ログ行を 'NSLog(@" Verification .....%@ "、self.delegate)に変更するとどうなりますか? –

+0

@PhillipMills私は「検証......(ヌル)」を参照してください。 –

答えて

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]; 

次に、以前と同じようにメソッドを呼び出します。

この例のいずれかの点が不足していないかどうかを確認してください。

+0

setdelegateはどこで定義されていますか?このメソッドを記述する必要がありますか? – drlobo

+0

代理人がプロパティとして設定されているため、その必要はありません。 – zahreelay

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]; 
} 
+0

私はすでにそれをしています。私はFirstViewControllerからotherMethodを呼び出すときに呼び出されません。 –

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"); 
    } 
} 
関連する問題