使用代理在视图控制器之间进行通信

时间:2012-07-24 13:35:37

标签: ios

在询问了一些问题之后,我学会了如何将命令从一个视图控制器发送到另一个视图控制器并设法将代码写入其工作但没有任何反应......

在我的项目中,我有两个名为sayfa1sayfa23的视图控制器。单击sayfa1处的按钮时,它将打开sayfa23并在标签上写(随机问候,请参阅下面的代码),但它没有发生。在模拟器上,该按钮只会打开sayfa23,而且标签上没有任何内容。如果你看一下代码,你可以更好地理解它。

sayfa1.h

#import <UIKit/UIKit.h>

@protocol sayfa1Delegate <NSObject>

- (void)dealWithButton1;

@end


@interface sayfa1 : UIViewController

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

@end

sayfa1.m

#import "sayfa1.h"

@interface sayfa1 ()

@end

@implementation sayfa1

@synthesize delegate;

-(IBAction)button
{
    [delegate dealWithButton1];
}

@end

sayfa23.h

#import <UIKit/UIKit.h>
#import "sayfa1.h"

@interface sayfa23 : UIViewController <sayfa1Delegate> 
{
    IBOutlet UILabel *label;
    sayfa1 *vc1 ;
}

@end

sayfa23.m

#import "sayfa23.h"
#import "sayfa1.h"

@interface sayfa23 ()

@end

@implementation sayfa23

- (void)dealWithButton1
{
    vc1.delegate = self;    
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
    { 
        label.text = @"hello1";    
    }
    else if (random_num == 2)
        label.text = @"hello2";
    else if (random_num == 3)
        label.text = @"hello3";
    else if (random_num == 4)
        label.text = @"hello4";
}

@end

在编写此代码后,我将按钮连接到sayfa23,这样它就会打开新页面,我也会将该按钮连接到sayfa1以接收按钮操作,并且我连接了标签(在sayfa23上)sayfa23接收标签订单。但正如我所说,没有任何错误,也没有问好我做错了什么?我在我的一些h文件的顶部导入了sayfa1.hsayfa23.h,导致Xcode给我一个关于未定义的错误并解决了这个问题,但这是我的错误或其他原因。

我想要的例子。

  • 用户打开应用

  • 屏幕上显示
  • sayfa1

  • 用户点击按钮,sayfa23显示sayfa23上的标签文字由sayfa1按钮改变,随机写入hello1..2..3等...

我做错了什么?

3 个答案:

答案 0 :(得分:2)

重新阅读您的问题,您询问第一个视图控制器如何打开第二个视图控制器并设置文本框。如果这确实是你想要做的事情,那么这是一个更简单的问题,根本不需要代表协议或代表。

前两个答案是通过对代表的讨论得出的,但这是为了解决不同的问题。只有当您需要第二个控制器将某些东西传回第一个控制器时,才需要代表。但是如果你只是想让你的第二个控制器从第一个控制器接收一些东西,那么就像这样简单:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

实现如下:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (NSString *)generateRandomText
{
    NSString *result;

    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        result = @"hello1";    
    else if (random_num == 2)
        result = @"hello2";
    else if (random_num == 3)
        result = @"hello3";
    else if (random_num == 4)
        result = @"hello4";

    return result;
}

// if you're using NIBs, it might be something like...
// you only need this method if you're using NIBs and you've manually hooked a button up to this
// if you're using segues, get rid of `goToNextViewController` and just use the following `prepareForSegue

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.textFromParent = [self generateRandomText];
    [self.navigationController pushViewController:secondController animated:YES];
}

// if you're using segues, give your segue an identifier, e.g. toSecondViewSegue, in Interface Builder and reference the exact same identifier here
// if you're not using segues, you don't need this prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toSecondViewSegue"])
    {
        SecondViewController *destinationController = segue.destinationViewController;

        destinationController.textFromParent = [self generateRandomText];
    }
}

@end

你的第二个控制器可能看起来像:

//  SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (strong, nonatomic) NSString *textFromParent;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

使用类似的实现:

//  SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize textFromParent = _textFromParent;
@synthesize label = _label;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.label.text = self.textFromParent;
}

@end

答案 1 :(得分:1)

第一个控制器在实例化第二个控制器时应将第二个委托设置为指向第一个视图控制器。因此,您的第一个视图控制器可能如下所示:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate <NSObject>

- (void)dealWithButton;

@end

@interface FirstViewController : UIViewController <FirstViewControllerDelegate>

@end

实现如下:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.delegate = self;
    [self.navigationController pushViewController:secondController animated:YES];
}

- (void)dealWithButton
{
    NSLog(@"Dealt with button from second controller");
}

@end

你的第二个控制器可能看起来像:

//  SecondViewController.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@class FirstViewController;

@interface SecondViewController : UIViewController

@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)buttonPressed:(id)sender;

@end

使用类似的实现:

//  SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize delegate = _delegate;
@synthesize label = _label;

- (IBAction)buttonPressed:(id)sender
{
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        self.label.text = @"hello1";    
    else if (random_num == 2)
        self.label.text = @"hello2";
    else if (random_num == 3)
        self.label.text = @"hello3";
    else if (random_num == 4)
        self.label.text = @"hello4";

    [self.delegate dealWithButton];
}
@end

<强>更新

您的原始问题并未明确表示您是希望标签位于第一个控制器上还是第二个控制器上。我上面的回答假设你想要它在第二个控制器上,但回想起来,你可能想要它在第一个控制器(代表)上。如果是这样,以下代码就是这样做的。请注意,我不只是更新dealWithButton中第一个视图控制器的标签,因为这很危险,因为您不知道视图是否可见(如果您收到didReceiveMemoryWarning,则可能已卸载)。所以我等待viewWillAppear。再次,第一个视图控制器:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate <NSObject>

- (void)dealWithButton;

@end

@interface FirstViewController : UIViewController <FirstViewControllerDelegate>

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

@end

及其实施:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
{
    NSString *_labelText;
}
@end

@implementation FirstViewController

@synthesize label = _label;

// if you're using storyboards, it would be like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"delegateSegue"])
    {
        SecondViewController *destinationController = segue.destinationViewController;
        FirstViewController *sourceController = segue.sourceViewController;

        destinationController.delegate = sourceController;
    }
}

// if not using storyboards, you probably have a button like:

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.delegate = self;
    [self.navigationController pushViewController:secondController animated:YES];
}

- (void)dealWithButton
{
    // note, because this is being called by the second view controller, you should *not* update the UI
    // directly, because you can't be assured this view controller's view is still in memory (if you got
    // a didReceiveMemoryWarning while on the second view controller, this first view controller will 
    // stay in memory, but its view could have been released). So save what you want the label to be,
    // and update it on viewWillAppear (and if the view was released, it will be reloaded by the time
    // you hit viewWillAppear.
    //
    // clearly, if you were doing view controller containment and this was the parent view, you wouldn't
    // want to do this. But I assume you're dealing with a simple push/present view controller situation.

    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        _labelText = @"hello1";    
    else if (random_num == 2)
        _labelText = @"hello2";
    else if (random_num == 3)
        _labelText = @"hello3";
    else if (random_num == 4)
        _labelText = @"hello4";

    NSLog(@"Dealt with button from second controller");
}

- (void)viewWillAppear:(BOOL)animated
{
    self.label.text = _labelText;
}

@end

第二个视图控制器:

//  SecondViewController.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@class FirstViewController;

@interface SecondViewController : UIViewController

@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;

- (IBAction)buttonPressed:(id)sender;

@end

及其实施:

//  SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize delegate = _delegate;

- (IBAction)buttonPressed:(id)sender
{
    [self.delegate dealWithButton];
}
@end

答案 2 :(得分:0)

尝试将以下方法添加到sayfa23的实现中:

- (void)viewDidLoad
{
    vc1 = [[sayfa1 alloc] init];
    vc1.delegate = self;
}

并删除vc1.delegate = self;来自你的dealWithButton1方法。

修改: 您必须了解方法dealWithButton1永远不会被调用,因为您永远不会将消息发送到对象。因此,您永远不会设置vc1的委托。使用viewDidLoad方法进行一些设置是一个很好的方法,该方法在加载视图时调用。在那里你可以分配sayfa1类的init(创建一个实例),并将它分配给你的属性vc1。分配对象后,您可以向其发送消息。您可以设置委托。

sayfa23.h

#import <UIKit/UIKit.h>
#import "sayfa1.h"

@interface sayfa23 : UIViewController <sayfa1Delegate> 



{

    IBOutlet UILabel *label;
}
    @property (nonatomic, strong) sayfa1 *vc1 ;



@end

sayfa23.m

#import "sayfa23.h"
#import "sayfa1.h"

@interface sayfa23 ()

@end

@implementation sayfa23
@synthesize vc1;


- (void)viewDidLoad
{
    vc1 = [[sayfa1 alloc] init];
    vc1.delegate = self;
}

- (void)dealWithButton1

{
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
    { 
        label.text = @"hello1";    
    }
    else if (random_num == 2)
        label.text = @"hello2";
    else if (random_num == 3)
        label.text = @"hello3";
    else if (random_num == 4)
        label.text = @"hello4";
}


@end