使用委托在视图控制器之间进行通信 [英] Use of Delegates to Communicate Between View Controllers

查看:53
本文介绍了使用委托在视图控制器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问了一些问题之后,我学习了如何将命令从一个视图控制器发送到另一个视图控制器,并设法编写了可以正常工作的代码,但是什么也没发生...

After asking some questions, I learned how to send orders from one view controller to another and managed to write the code its working but nothing happens...

在我的项目中,我有两个名为sayfa1sayfa23的视图控制器.单击sayfa1处的按钮时,它将打开sayfa23并在标签上书写(随机,您好,请参见下面的代码),但没有发生.在模拟器上,该按钮仅打开sayfa23,这没什么可标记的.如果您看一下代码,将会更好地理解它.

In my project I have two view controllers named as sayfa1 and sayfa23. When a button at sayfa1 is clicked it will open up sayfa23 and write on a label (random hello see the code below) but its not happening. On simulator that button only opens up the sayfa23 and thats it nothing happening to label. If you look at the code you can understand it better.

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

sayfa23.h

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

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

@end

sayfa23.m

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给我一个有关未定义的错误,并解决了该问题,但这是我的错误还是其他错误.

After writing this code I connected the button to sayfa23 so it will open the new page also I connected that button to sayfa1 to receive button actions and I connected label (on sayfa23) to sayfa23 receive label orders. But as I say nothing happens no errors and no hello what I am doing wrong? I imported sayfa1.h or sayfa23.h at the top of some of my h files cause Xcode give me an error about not defined and solved that problem but is it my mistake or something else.

我想要的例子.

  • 用户打开应用

  • User opens the app

sayfa1

用户单击按钮,并显示sayfa23,显示sayfa23上的标签文本已由sayfa1处的按钮更改,它会写上随机的hello1..2..3等...

User clicks the button and sayfa23 is shown the label text on sayfa23 is changed by the button which is at sayfa1 it write random hello1..2..3 etc...

我做错了什么?

推荐答案

重新阅读您的问题,您询问第一个视图控制器如何打开第二个视图控制器并设置文本框.如果确实是您要尝试执行的操作,那么这将是一个简单得多的问题,根本不需要委托协议或委托.

Rereading your question, you ask how your first view controller can open the second view controller and set a text box. If that is, indeed, what you are trying to do, it's a far simpler question, no delegate protocol or delegates required at all.

前面的两个答案是由代表的讨论提供的,但这只是为了解决另一个问题.仅当您需要第二个控制器将某些内容传递回第一个控制器时,才需要委托.但是,如果您只想让第二个控制器从第一个控制器接收某些信息,则非常简单:

The two previous answers were informed by the discussion of delegates, but that's designed to solve a different problem. Delegates are only required if you need your second controller to pass something back to the first controller. But if you just want your second controller to receive something from the first controller, it's as simple as:

//  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

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

And your second controller might look like:

//  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

这篇关于使用委托在视图控制器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆