Objective c - pass back string value using delegate

▼魔方 西西 提交于 2019-12-20 07:40:21

问题


I want to pass UITextField *nameText from GreenViewController to UILabel *nameValue in ViewController using delegate.

I made delegate, the onSave method in ViewController is called but the screen is not going back and the name is not passed. What is the problem? Here are the header and impelentation files:

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

回答1:


I assume you didnt set the delegate .

Set it like this:

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

  myVc.delegate = self;

Put this in your view controller view did load method!!!




回答2:


try with GreenViewController.h:

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

and GreenViewController.m:

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

ViewController.* seems to be OK



来源:https://stackoverflow.com/questions/33925086/objective-c-pass-back-string-value-using-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!