2013-07-29 99 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委托”来合成委托。