目标C-Custom代表不工作

时间:2015-10-29 11:23:42

标签: ios objective-c delegates

我正在尝试使用委托方法从视图控制器获取字符串到另一个。但委托方法没有被调用。下面是代码

@protocol CustomDelegate<NSObject>
  -(void)didDataRecieved;
@end 

@interface CustomController:UIViewController
@property id<CustomDelegate>delegate;
@property(retain,nonatomic)NSString *string;
@end

@implementaion CustomController
-(void)viewDidLoad
  {
    string=@"hello";
    if([self.delegate respondsToSelector@selector(didDataRecived)]) {
       [self.delegate didDataRecieved];
    }
  }

-(IBACTION)gotoViewController
 {
   ViewController *view=[self.storyboard instantiateViewController:@"View"]; 
   [self.navigationController pushViewController:view aniamted:YES];
 }
@end

@interface ViewController:UIViewController<CustomDelegate>
@property (nonatomic,retain)CustomController *cust;
@end

@implementation ViewController
-(void)viewDidLoad
 {
   self.cust=[[CustomController alloc]init];
   self.cust.delegate=self;
 }

-(void)didDataRecieved
 {
  NSLog(@"data %@",self.cust.string);
 }
@end

任何人都可以指出哪里出错......请帮助。

编辑代码..也就是这样。

  if([self.delegate respondsToSelector@selector(didDataRecived)]){
    [self.delegate didDataRecieved];
   }

2 个答案:

答案 0 :(得分:1)

我将为您提供示例编码。自定义以下代码。

这里我们有两个视图控制器。

的ViewController 和 SecondViewController

在SecondViewController中

·H

#import <UIKit/UIKit.h>
@class SecondViewController;
@protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, assign)id<SecondViewControllerDelegate> delegate;  
@property (nonatomic, strong) IBOutlet UITextField *nameTextField;//It must connect as outlet connection
- (IBAction)doneButtonTapped:(id)sender;
@end

的.m

#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
    // Custom initialization
   }
   return self;
}

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view.
}

//Either use NSNotification or Delegate
- (IBAction)doneButtonTapped:(id)sender;
{
          //Use Notification
     [[NSNotificationCenter defaultCenter] postNotificationName:@"passingDataFromSecondViewToFirstView" object:self.nameTextField.text];
          //OR Custom Delegate
     [self.delegate secondViewController:self didEnterText:self.nameTextField.text];
     [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
}
ViewController中的

·H

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<SecondViewControllerDelegate>
@property (nonatomic, strong) IBOutlet UILabel *labelName; //You must connect the label with outlet connection
- (IBAction)gotoNextView:(id)sender;
@end

的.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
   [super viewDidLoad];
   //addObserver here...
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFromPreviousViewControllerNotificationReceived:) name:@"passingDataFromSecondViewToFirstView" object:nil];
   // Do any additional setup after loading the view, typically from a nib.
}

//addObserver Method here....
- (void)textFromPreviousViewControllerNotificationReceived:(NSNotification *)notification
{
   // set text to label...
   NSString *string = [notification object];
   self.labelName.text = string;
}
- (IBAction)gotoNextView:(id)sender;
{
   //If you use storyboard
   SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
   //OR  If you use XIB
   SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
   secondViewController.delegate = self;
   [self.navigationController pushViewController:secondViewController animated:YES];
}

//Calling custom delegate method
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text
{
   self.labelName.text = text; //Getting the data and assign the data to label here.
}

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

为了理解代码,我创建了一个从第二个视图控制器到第一个视图控制器的简单传递数据。

首先,我们将视图从第一个视图控制器导航到第二个视图控制器。

之后,我们将数据从第二个视图控制器发送到第一个视图控制器。

注意:您可以使用NSNotification或自定义委派方法将数据从One View Controller发送到其他视图控制器

如果您使用NSNotification,则需要设置 postNotificationName 以获取按钮操作方法中的数据。

接下来,您需要编写 addObserver (将数据发送到所需的View Controller)ViewController并在同一个View Controller中调用addObserver方法。

如果您使用自定义委托,  通常我们选择自定义协议代理,我们还需要分配代表。

非常重要的是,我们必须在第二视图控制器中设置自定义委托方法。因为我们在单击完成按钮后将数据发送到第一个视图控制器的位置第二视图控制器。

最后,我们必须在第一个视图控制器中调用自定义委派方法,在那里我们获取数据并将该数据分配给label.Now您可以使用查看传递的数据自定义代表。

同样,您可以使用自定义委托方法

将数据发送到其他视图控制器

答案 1 :(得分:0)

你如何推动你的第二个控制器?我看不到。 但是你的代码适合我。

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewController1 *vc = [ViewController1 new];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)didDataRecieved
{
    NSLog(@"recieved");
}

@end