2013-07-29 97 views
0

我使用委託mehod在視圖控制器之間傳遞數據。這不起作用。委託方法不工作ios

@protocol PassCountry <NSObject> 

@required 
- (void) setPickedCountry:(NSString *)pickedCountry; 
@end 

@interface SelectCountryViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> { 
    id <PassCountry> delegate; 
} 

@property (copy) NSString *pickedCountry; 
@property (retain) id delegate; 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent (NSInteger)component { 
    pickedCountry = [self.countries objectAtIndex:row]; 
} 


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return self.countries.count; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [[self delegate] setPickedCountry:pickedCountry]; 
} 
+4

它究竟如何不工作? – Kreiri

+1

你在哪裏調用這個委託方法,顯示代碼 –

+1

你在哪裏設置委託,顯示代碼以及 – John

回答

2
#import <UIKit/UIKit.h> 
    @protocol PassCountry <NSObject> 

    @required 
    - (void) setPickedCountry:(NSString *)pickedCountry; 
    @end 

    @interface secondViewViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> 
    { 
     id <PassCountry> delegate; 

    IBOutlet UIButton *aButton; 
    } 

@property (copy) NSString *pickedCountry; 
@property (assign) id<PassCountry> delegate; // for delegate use assign don't retain 

// in another class you are creating instance of this class 

    secondViewViewController *secController = [[secondViewViewController alloc]init]; 
    secController.delegate = self;//check this added or not 
    [self presentViewController:secController animated:YES completion:nil]; 

    //and implementation of deleagte method 
    - (void) setPickedCountry:(NSString *)pickedCountry 
    { 
     // do some stuff 

    } 
1

試試這個::

.h文件中

@protocol delegateTextSize <NSObject> 

@optional 
-(void)selectedTextSize:(double)textSize; 
@end 

@interface CustomFontSizeCell : UITableViewCell 

@property (nonatomic,retain) id delegateTextSize; 
-(IBAction)changeSize:(id)sender; 

@end 

.m文件

-(IBAction)changeSize:(id)sender 
{ 
    [delegateTextSize selectedTextSize:app.selectedFontSize]; 
} 

何處使用,

.h文件中

Controller <delegateTextSize> 

.m文件

-(void)selectedTextSize:(double)textSize 
{ 
} 

希望,這將工作

感謝。

+0

Downvoting的內存含義需要的結果是被保留的委託對象。 –

1

首先,委託實例不能保留。 其次,在調用方法[self delegate]之前,應該使用「@synthesize委託」來合成委託。