【问题标题】:Objective c - pass back string value using delegateObjective c - 使用委托传回字符串值
【发布时间】:2015-11-25 19:39:00
【问题描述】:

我想使用委托将 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

【问题讨论】:

  • 你在哪里设置了代理?
  • 这是我的全部代码,不设置委托可能是我的问题。我不知道该怎么做以及在哪里写。我很高兴你能帮助@Mr.T

标签: ios objective-c iphone delegates


【解决方案1】:

我假设你没有设置委托。

这样设置:

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

  myVc.delegate = self;

把这个放在你的视图控制器视图做加载方法!!!

【讨论】:

  • 我在等待答案时这样做了(在互联网上找到了您的代码),但不知何故程序无法到达 ViewController 中的 onSave 方法。它不在控制台上打印: NSLog(@"onSave in parent controller"); @Mr.T
  • 如何回到视图控制器?您是否按下后退按钮以从 greenview 移动到视图控制器?
  • 我按下连接到 GreenViewController 上带有插座的保存方法的保存按钮。这个方法在我在 GVC.m @Mr.T 中的代码上
  • 我在 prepareForSegue 方法中添加了:“gVC.delegate = self”,现在我可以在 onSave 中看到 nslog(调用了父函数),但屏幕没有返回并将字符串发回 @ T先生
【解决方案2】:

尝试使用 GreenViewController.h:

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

和 GreenViewController.m:

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

ViewController.* 似乎没问题

【讨论】:

  • Unofrunately 它不起作用@disepulv 我真的很感谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 2014-11-16
  • 2015-12-19
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多