使用委托方法

时间:2017-03-15 13:48:46

标签: ios objective-c delegates protocols uistoryboardsegue

我有两个viewController和iam使用委托方法更新第一个视图控制器中的标签,并在第二个视图控制器中输入文本。

我的问题是在textfield(secondViewController)中输入的值没有被推入UILabel(FirstVIewController)

我的第一个ViewController.h

#import "SecondViewController.h"
@interface ViewController : UIViewController  <CustomDelegate>
@end

First ViewController.m

@interface ViewController () 

@property (weak, nonatomic) IBOutlet UILabel *infoLabel;

@end

@implementation ViewController

-(void)sendinfo:(NSString *)info
    {

_infoLabel.text = info;

    }

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

   if ([[segue identifier] isEqualToString:@"info"])
      {
   SecondViewController *obj = segue.destinationViewController;

   obj.userDelegate = self;
      }

}

SecondViewController.h

@protocol CustomDelegate <NSObject>

-(void)sendinfo:(NSString *)info;

 @end

 @interface SecondViewController : UIViewController

 @property (weak, nonatomic) id <CustomDelegate> userDelegate;

 @property (weak, nonatomic) IBOutlet UITextField *textField;

SecondViewController.m

 - (IBAction)sendInfo:(id)sender {


     [self.userDelegate sendinfo:_textField.text];
   UIViewController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Home"];

[self presentViewController:controller animated:YES completion:Nil];
 }

1 个答案:

答案 0 :(得分:2)

您正在SecondViewController中创建另一个UIViewController,并且委托被设置为您来自的viewController。

要查看更改,您应该在SecondViewController中执行[self dismissViewControllerAnimated:YES completion:nil];

示例:

- (IBAction)sendInfo:(id)sender {

  [self.userDelegate sendinfo:_textField.text];

  [self dismissViewControllerAnimated:YES completion:nil];
}