Objective c - 使用委托传回字符串值

时间:2015-11-25 19:39:01

标签: ios objective-c iphone delegates

我想使用委托将UITextField * nameText从GreenViewController传递到ViewController中的UILabel * nameValue。

我创建了委托,调用了ViewController中的onSave方法,但屏幕没有返回,并且没有传递名称。 问题是什么? 以下是标题和紧急文件:

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

2 个答案:

答案 0 :(得分:0)

我假设您没有设置委托。

设置如下:

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

  myVc.delegate = self;

把它放在你的视图控制器视图中加载方法!!!

答案 1 :(得分:-1)

尝试使用GreenViewController.h:

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

和GreenViewController.m:

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

ViewController。*似乎没问题