2012-05-12 61 views
1

我有一個UIButton調用另一個方法UIViewController,該方法調用委託方法,這是不發生。調用委託方法不工作

喜歡的東西:

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我看到「Verification ......(null)」。 –

回答

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"); 
    } 
}