2015-11-25 33 views
-1

我想使用委託在ViewController中將UITextField * nameText從GreenViewController傳遞給UILabel * nameValue。目標c - 使用委託傳回字符串值

我做了委託,調用了ViewController中的onSave方法,但屏幕沒有回去,名字沒有傳入。 問題是什麼? 這裏是頭部和impelentation文件:

GreenViewController.h

#import <UIKit/UIKit.h> 

@protocol GreenViewDelegate <NSObject> 
-(void)onSave:(NSString*)nameValue; 

@end 


@interface GreenViewController : UIViewController 

@property id<GreenViewDelegate> delegate; 
@property (nonatomic) NSString* sliderValuePassed; 
@property (weak, nonatomic) IBOutlet UIButton *Next; 
@property (weak, nonatomic) IBOutlet UIButton *Save; 
@property (weak, nonatomic) IBOutlet UIButton *Back; 
@property (weak, nonatomic) IBOutlet UILabel *sliderTextValue; 
@property (weak, nonatomic) IBOutlet UITextField *NameText; 

- (IBAction)save:(id)sender; 

@end 

GreenViewController.m

#import "GreenViewController.h" 
#import "ViewController.h" 

@interface GreenViewController() 

@end 

@implementation GreenViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.sliderTextValue.text = self.sliderValuePassed; 
} 
- (IBAction)back:(id)sender { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

- (IBAction)save:(id)sender { 
    [self.delegate onSave:self.NameText.text]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

ViewController.h

#import <UIKit/UIKit.h> 
#import "GreenViewController.h" 

@interface ViewController : UIViewController <GreenViewDelegate> 
@property (weak, nonatomic) IBOutlet UISlider *slider; 
@property (weak, nonatomic) IBOutlet UILabel *sliderTextValue; 
@property (weak, nonatomic) IBOutlet UIButton *EnterName; 
@property (weak, nonatomic) IBOutlet UILabel *NameValue; 
@property (weak, nonatomic) IBOutlet UIButton *LikeIOS; 
@property (weak, nonatomic) IBOutlet UISwitch *CheckboxIOS; 

@end 

ViewController.m

#import "ViewController.h" 
#import "RedViewController.h" 
#import "GreenViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.sliderTextValue.text = [NSString stringWithFormat:@"Slider value = %d",(int)self.slider.value]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([segue.identifier isEqualToString:@"yellowToGreen"]){ 
     GreenViewController* gVC = segue.destinationViewController; 
     gVC.sliderValuePassed = [NSString stringWithFormat:@"Slider value = %d",(int)self.slider.value]; 
     gVC.delegate = self; 
    }else if([segue.identifier isEqualToString:@"yellowToRed"]){ 
     RedViewController* rVC = segue.destinationViewController; 
     rVC.sliderValuePassed = [NSString stringWithFormat:@"Slider value = %d",(int)self.slider.value]; 
     rVC.CheckboxIOS = self.CheckboxIOS; 
    } 
} 

- (IBAction)sliderChangeValue:(id)sender { 
    int sliderVal = self.slider.value; 
    self.sliderTextValue.text = [NSString stringWithFormat:@"Slider value = %d",sliderVal]; 
} 

-(void)onSave:(NSString*)nameValue{ 
    NSLog(@"onSave in father controller"); 
    self.NameValue.text = [NSString stringWithFormat:@"Name = %@",nameValue]; 
} 

@end 
+0

你在哪裏設置委託? –

+0

這是我所有的代碼,而不是設置委託可能是我的問題。 我不知道該怎麼做,寫在哪裏。我會很高興爲你提供幫助@T –

回答

0

我假設你沒有設置委託。

設置這樣的:

GreenViewController *myVc = [[GreenViewController alloc] init]; 

    myVc.delegate = self; 

在您的視圖控制器視圖將這個做負載方法!

+0

當我等待一個答案(在網上找到你的代碼)時,我做了它,但不知何故程序沒有達到ViewController中的onSave方法。 它不在控制檯上打印:NSLog(@「onSave in father controller」); @ Mr.T –

+0

你如何回到視圖控制器?你按下後退按鈕,從綠景移到視圖控制器? –

+0

我按下連接的保存按鈕,在帶有插座的GreenViewController上保存方法。這個方法是在我的代碼中的GVC.m @ Mr.T –

-1

嘗試GreenViewController.h:

@property (nonatomic, assign) id<GreenViewDelegate> delegate; 

和GreenViewController.m:

的ViewController *似乎是OK

+0

非常幸運,它不起作用@disepulv 我真的很感謝你的幫助 –